Format string
[2005-12-11 11:21:32 | Author:
ClyingBoy]
:
|
|
The .NET Framework provides customized, a formatting mechanism suitable for conventional use, which converts values to a string suitable for display. For example, the numerical format can be formatted into hexadecimal, scientific counting, or a series of numbers that are divided by the punctuation symbol specified by the user. Date and time can be formatted into specific countries, regions or regions. The enumeration can be formatted to its value or name.
You can control formatted by specifying format strings and format providers or using default settings. The format string contains one or more format specifiers characters to indicate how to convert values. The format provider provides information on other control, replacement, and regional aspects required for the conversion of specific types.
You can rewrite the method of the format string by implementing the iFormatTable interface; providing your own format provider by implementing the IFORMATPROVIDER interface; perform your own formatting by implementing the ICUSTOMMATTER interface.
.NET Framework provides a composite formatted feature that embeds one or more formatted values into the output string using one or more format strings. Output strings can be used for further processing, displayed to the system console or write to stream.
Format specifier
.NET Framework defines a standard and custom format specifier for formatting numbers, dates, and time and enumeration. A variety of methods of formatting output strings (such as console.writeline, and all types of TOSTRING methods), and some methods of analyzing input strings (such as DateTime.ParseExact) use formatted specifiers.
For information on formatting digital data, see "Digital Format Strings". For a list of common digital format specifiers, see "Standard Digital Format Strings"; see "Custom Digital Format Strings" for custom format spectrices (available to create your own format string).
For information on formatting the date and time, see "Date and Time Format Strings". For a list of common date and time format specifiers, see "Standard DateTime Format string"; see "Custom DateTime Format) for a list of custom time and date format specifiers (available to create your own format string). String ".
For information on formatting enumerations, as well as a list of standard enumeration formats, see "Enumerate String".
Analysis and format specifier
Formatting translates type value into a string representation; analysis is the opposite of formatting, which is created from a string representation. The format provider controls how to perform an analysis, some methods (such as DateTime.Parsexact) use format specifier parameters to indicate the intended format of the string representation. For more information on the analysis, see "Analysis Strings".
Tostring and format specifier
.NET Framework supports the default Tositring method of the overload type. This method performs the basic list of TOSTRING, which uses the format specifier parameter to indicate how to format the value. For more information, see "Formatting Class Types" and IFORMATTABLE interface.
The FORMAT function returns a string setting format in the format String expression.
1.
<% @ Page language = "c #"%>
Protected void Page_load (Object Sender, Eventargs E)
{
String a = "clingingboy"; response.write (String.Format ("Your name is: {0}", a)); // {0} represents the placeholder, passed to the second parameter to replace the account Bitter
String name = "clingingboy";
String Age = "20";
String city = "hangzhou";
Response.write ("
");
Response.write (String.Format ("My name is {1}, age is {0}, from {2}", age, name, city);
Response.write (String.Format ("
", "111")); // Occupy Width}
script>
Click to test
2. Format value
<% @ Page language = "c #"%>
Void Page_Load (Object Sender, Eventargs E)
{
IntNum;
Decimal Decnum;
INTNUM = 32001;
Decnum = 32001.9099m;
// Decimal format;
Response.write (INTNUM.TOSTRING ("D") "
");// => Displays 32001
// Exponential Format
Response.write (INTNUM.TOSTRING ("E") "
");/ / => Displays 3.200100E 004
Response.write (Decnum.toString ("E") "
");// => displays 3.200191e 004
// Fixed-Point Format
Response.write (INTNUM.TOSTRING ("f") "
");// => Displays 32001.00
Response.write (Decnum.toString ("f") "
");/ / => Displays 32001.91
// general format
Response.write (INTNUM.TOSTRING ("G") "
");// => Displays 32001
Response.write (Decnum.toString ("G") "
");// => displays 32001.9099
// Number Format
Response.write (Intnum.Tostring ("N") "
");/ / => Displays 32,001.00
Response.write (Decnum.toString ("n") "
"); // => Displays 32,001.91Response.write (INTNUM.TOSTRING ("X") "
");// 7d01
}
script>
Click to test
Format using format
Response.write (String.Format ("{0: N}", 123456) "
);<% @ Page language = "c #"%>
Void Page_Load (Object Sender, Eventargs E)
{
IntNum;
Decimal Decnum;
INTNUM = 32001;
Decnum = 32001.9099m;
// Decimal format;
Response.write (String.Format ("{0: N}", 123456) "
"); // 0 is the index value, n is formattedResponse.write (INTNUM.TOSTRING ("D10") "
"); // Add 0 in front of the value, the total number is 10Response.write (INTNUM.TOSTRING ("N10") "
"); // Decan}
script>
Click to test
3. Format money
<% @ Page language = "c #"%>
Void Page_Load (Object Sender, Eventargs E)
{
Decimal intNum;
INTNUM = 32001.4633M;
// Decimal format;
Response.write (String.Format ("{0: C}", INTNUM) "
"); // 0 is the index value, N is formattedResponse.write (String.Format ("{0: C3}", intNum) "
");Response.write (INTNUM.TOSTRING ("C") "
"); / / The default count number is 2Response.write (Intnum.toString ("C1") "
"); // Automatic four}
script>
Click to test
4. Format date and time
<% @ Page language = "c #"%>
body>
Void Page_Load (Object Sender, Eventargs E)
{
DateTime DTMDATE; DTMDATE = DATETIME.NOW;
// Short Date
Response.write (DTMDATE.TOSTRING ("D"));
Response.write ("
");/ / => Displays 2/13/2001
// long Date
Response.write (DTMDATE.TOSTRING ("D"));
Response.write ("
");// => Displays Tuesday, February 13, 2001
// long date and short time
Response.write (DTMDATE.TOSTRING ("F"));
Response.write ("
");// => Displays Tuesday, February 13, 2001 3:40 PM
// long date and long time
Response.write (DTMDATE.TOSTRING ("F"));
Response.write ("
");// => Displays Tuesday, February 13, 2001 3:41:04 PM
// short date and short time
Response.write (DTMDATE.TOSTRING ("G");
Response.write ("
");/ / => Displays 2/13/2001 3:42 PM
// short date and long time
Response.write (DTMDATE.TOSTRING ("G");
Response.write ("
");// => displays 2/13/2001 3:42:52 PM
// Month and day
Response.write (DTMDATE.TOSTRING ("M");
Response.write ("
");// => Displays february 13
// RFC 1123
Response.write (DTMDATE.TOSTRING ("R");
Response.write ("
");// => displays tue, 13 feb 2001 15:44:52 GMT
// ISO 8601
Response.write (DTMDATE.TOSTRING ("S"));
Response.write ("
");// => displays 2001-02-13t15: 45: 55
// Short Time
Response.write (DTMDATE.TOSTRING ("T");
Response.write ("
");// => Displays 3:47 PM
// long time
Response.write (DTMDATE.TOSTRING ("T");
Response.write ("
");// => displays 3:47:37 PM // universal iso 8601
Response.write (DTMDATE.TOSTRING ("U"));
Response.write ("
");/ / => Displays 2001-02-13 15: 48: 41Z
// universal time
Response.write (DTMDATE.TOSTRING ("U"));
Response.write ("
");// => displays Tuesday, February 13, 2001 11:49:51 PM
// Month and Year
Response.write (DTMDATE.TOSTRING ("y"));
Response.write ("
");// => displays february, 2001
}
script>
Click to test