[C #]. ToString () formatting

Source: Internet
Author: User
Tags alphabetic character

C # ToString () format string formatted numeric value: Sometimes, we may need to format the numeric values in a certain format. We use the format string to specify the format. The format string takes the form: Axx, where a is the format specifier, specifies the format type, and xx is the precision specifier that controls the number of significant digits or decimal digits of the formatted output.
format Specifier Declaration example output
C Currency 2.5.ToString ("C") ¥2.50
D decimal number 25.ToString ("D5") 00025
e-Scientific 25000.ToString ("E") 2.500000E+005
F fixed point 25.ToString ("F2") 25.00
G Regular 2.5.ToString ("G") 2.5
N Digital 2500000.ToString ("n") 2,500,000.00
X hex 255.ToString ("x") FF

String.Format formatting examples for C #
Stringstr1 =string.               Format ("{0:n1}", 56789); result:56,789.0
STRINGSTR2 =string.               Format ("{0:n2}", 56789); result:56,789.00
STRINGSTR3 =string.               Format ("{0:n3}", 56789); result:56,789.000
Stringstr8 =string.               Format ("{0:f1}", 56789); result:56789.0
STRINGSTR9 =string.               Format ("{0:f2}", 56789); result:56789.00
Stringstr11 = (56789/100.0).           ToString ("#.##"); result:567.89
Stringstr12 = (56789/100).             ToString ("#.##"); result:567

C or C
Currency
Console.Write ("{0:c}", 2.5); $2.50
Console.Write ("{0:c}",-2.5); ($2.50)

D or D
Decimal number
Console.Write ("{0:d5}", 25); 00025

E or E
Scientific type
Console.Write ("{0:e}", 250000); 2.500000E+005

F or F
Fixed point
Console.Write ("{0:f2}", 25); 25.00
Console.Write ("{0:f0}", 25); 25

G or G
Conventional
Console.Write ("{0:g}", 2.5); 2.5

N or N
Digital
Console.Write ("{0:n}", 2500000); 2,500,000.00

X or X
Hexadecimal
Console.Write ("{0:x}", 250); FA
Console.Write ("{0:x}", 0xFFFF); FFFF

C # Numeric formatted string 2010-07-03 08:59 a.m. a standard numeric format string is used to format a common numeric type. The standard numeric format string takes the form of Axx, where a is an alphabetic character called the format specifier, and xx is an optional integer called the precision specifier. The precision specifier ranges from 0 to 99 and affects the number of digits in the result. Any numeric format string that contains more than one alphabetic character (including whitespace) is interpreted as a custom numeric format string.

Found online as long as you go to Baidu Google search, the article is the same, excerpts from each other, plagiarism. There are some errors, so the code, and the code for my other articles, are validated after running.

Using System;


Namespace Tostringformat
{
Class Program
{
static void Main (string[] args)
{
According to the Chinese custom standard transformation
System.Globalization.CultureInfo culinfo = System.Globalization.CultureInfo.CreateSpecificCulture ("ZH-CN");


"C or C", the number is converted to a string representing the monetary amount. The conversion is controlled by the currency format information of the current NumberFormatInfo object.
The precision specifier indicates the desired number of decimal digits. If the precision specifier is omitted, the default currency precision given by the current NumberFormatInfo object is used.
Console.WriteLine ("(2.5)". ToString (' C '): "+ (2.5). ToString ("C", Culinfo)); ¥2.5
Console.WriteLine ("(2.5)". ToString (' C2 '): "+ (2.5). ToString ("C2", Culinfo)); ¥2.50
Console.WriteLine ("(2.5)". ToString (' C3 '): "+ (2.5). ToString ("C3", Culinfo)); ¥2.500
Console.WriteLine ("*************************************************************");
"D or D", this format is supported only for integer types. A string that is converted to a decimal number (0-9), preceded by a minus sign, if the number is negative.
The precision specifier indicates the minimum number of digits required in the resulting string. If necessary, fill the left side of the number with 0 to produce the number of digits given by the precision specifier.
Console.WriteLine ("(25)". ToString (' D '): "+ (25). ToString ("D", Culinfo)); 25
Console.WriteLine ("(25)". ToString (' D3 '): "+ (25). ToString ("D3", Culinfo)); 025
Console.WriteLine ("(25)". ToString (' D4 '): "+ (25). ToString ("D4", Culinfo)); 0025
Console.WriteLine ("(25)". ToString (' D5 '): "+ (25). ToString ("D5", Culinfo)); 00025
Console.WriteLine ("*************************************************************");
"E or E", the number is converted to "-d.ddd ... A string in the form of e+ddd "or"-d.ddd...e+ddd ", where each" D "represents a number (0-9). If the number is negative, the string begins with a minus sign. There is always a number before the decimal point. The precision specifier indicates the number of digits required after the decimal point. If the precision specifier is omitted, the default value, six digits after the decimal point, is used.
The case of the format specifier indicates whether to prefix "E" or "E" before the exponent. The index is always composed of a plus or minus sign and a minimum of three digits. If required, fill the index with 0 to meet the requirement of a minimum of three digits.
Console.WriteLine ("(25000)". ToString (' E '): "+ (25000). ToString ("E", Culinfo)); 2.500000E+004
Console.WriteLine ("(25000)". ToString (' E2 '): "+ (25000). ToString ("E2", Culinfo)); 2.50E+004
Console.WriteLine ("(25000)". ToString (' E3 '): "+ (25000). ToString ("E3", Culinfo)); 2.500E+004
Console.WriteLine ("(25000)". ToString (' e '): "+ (25000). ToString ("E", Culinfo)); 2.500000e+004
Console.WriteLine ("(25000)". ToString (' E2 '): "+ (25000). ToString ("E2", Culinfo)); 2.50e+004
Console.WriteLine ("(25000)". ToString (' E3 '): "+ (25000). ToString ("E3", Culinfo)); 2.500e+004
Console.WriteLine ("*************************************************************");
"F or F", the number is converted to a string in the form "-ddd.ddd ...", where each "D" represents a number (0-9). If the number is negative, the string begins with a minus sign.
The precision specifier indicates the desired number of decimal digits. If the precision descriptor is omitted, the default numeric precision is given by the Numberdecimaldigits property of the current NumberFormatInfo object.
Console.WriteLine ("(25)". ToString (' F0 '): "+ (25). ToString ("F0", Culinfo)); 25
Console.WriteLine ("(25)". ToString (' F1 '): "+ (25). ToString ("F1", Culinfo)); 25.0
Console.WriteLine ("(25)". ToString (' F2 '): "+ (25). ToString ("F2", Culinfo)); 25.00
Console.WriteLine ("*************************************************************");
"G or G", numbers are converted to the most compact form of fixed-point or scientific notation, depending on the type of number and whether there is a precision specifier. If the precision specifier is omitted or zero, the type of the number determines the default precision, as shown in the following table.
Byte or Sbyte:3
Int16 or Uint16:5
Int32 or Uint32:10
Int64 or uint64:19
Single:7
Double:15
Decimal:29
If the exponent is greater than 5 and less than the precision specifier when the number is expressed in scientific notation, the fixed-point notation is used; otherwise, scientific notation is used. If a decimal point is required, and trailing zeros are ignored, the result contains a decimal points. If the precision specifier exists and the number of significant digits in the result exceeds the specified precision, the excess trailing digits are removed by rounding.
If the number is Decimal and the precision specifier is omitted, there is an exception to the above rule. In this case, the fixed point notation is always used and trailing zeros are retained.
When using scientific notation, if the format specifier is "G", the exponent of the result is prefixed with "E", and if the format specifier is "G", the exponent of the result is prefixed with "E".
Console.WriteLine ("(2.5)". ToString (' G '): "+ (250). ToString ("G", Culinfo)); 250
Console.WriteLine ("(2.5)". ToString (' G1 '): "+ (250). ToString ("G1", Culinfo)); 3E+02
Console.WriteLine ("(2.5)". ToString (' G2 '): "+ (250). ToString ("G2", Culinfo)); 2.5E+02
Console.WriteLine ("(2.5)". ToString (' G3 '): "+ (250). ToString ("G3", Culinfo)); 250
Console.WriteLine ("*************************************************************");
"N or n", the number is converted to "-d,ddd,ddd.ddd ..." in the form of a string, where "-" represents a negative sign (if required), "D" represents a number (0-9), "," represents the thousands separator between numeric groups, "." Represents the decimal point symbol. The actual negative pattern, number group size, thousands separator, and decimal separator are separated by the current NumberFormatInfo object's Numbernegativepattern, numbergroupsizes, NumberGroupSeparator, and The NumberDecimalSeparator property is specified.
The precision specifier indicates the desired number of decimal digits. If the precision descriptor is omitted, the default numeric precision is given by the Numberdecimaldigits property of the current NumberFormatInfo object.
Console.WriteLine ("(250000)". ToString (' N '): "+ (250000). ToString ("N", Culinfo)); 250,000.00
Console.WriteLine ("(250000)". ToString (' N1 '): "+ (250000). ToString ("N1", Culinfo)); 250,000.0
Console.WriteLine ("(250000)". ToString (' N3 '): "+ (250000). ToString ("N3", Culinfo)); 250,000.000
Console.WriteLine ("*************************************************************");
"X or X" supports this format only for integer types. A string that converts a number to a hexadecimal number. The case of the format specifier indicates whether to use uppercase characters or lowercase characters for hexadecimal digits greater than 9. For example, use "X" to produce "ABCDEF" and "X" to produce "ABCDEF".
The precision specifier indicates the minimum number of digits required in the resulting string. If necessary, fill the left side of the number with 0 to produce the number of digits given by the precision specifier.
Console.WriteLine ("(25)". ToString (' X '): "+ (25). ToString ("X", Culinfo));//19
Console.WriteLine ("(25)". ToString (' X4 '): "+ (25). ToString ("X4", Culinfo));//19
Console.WriteLine ("(25)". ToString (' X8 '): "+ (25). ToString ("X8", Culinfo));//19
Console.WriteLine ("*************************************************************");

"P or P" follows NumberFormatInfo..::. The Percentnegativepattern property (if the number is negative) or NumberFormatInfo.::. Percentpositivepattern the definition of the property (if the number is positive), converts the number to a string representing the percentage. The converted number is multiplied by 100 to represent a percentage.
The precision specifier indicates the desired number of decimal digits. If the precision specifier is omitted, the default numeric precision given by the current NumberFormatInfo object is used.
Console.WriteLine ("(0.025)". ToString (' P '): "+ (0.025). ToString ("P", Culinfo));//2.50%
Console.WriteLine ("(0.025)". ToString (' P1 '): "+ (0.025). ToString ("P1", Culinfo));//2.5%
Console.WriteLine ("(0.025)". ToString (' P2 '): "+ (0.025). ToString ("P2", Culinfo));//2.50%
Console.WriteLine ("(0.025)". ToString (' P3 '): "+ (0.025). ToString ("P3", Culinfo));//2.500%
Console.WriteLine ("*************************************************************");
"R or R" supports this format only for single and Double types. The round-trip specifier guarantees that the numeric value converted to a string is parsed again into the same value. When you use this specifier to format a numeric value, you first test it with the general format: Double uses 15-bit precision, and single uses 7-bit precision. If this value is parsed back to the same value successfully, it is formatted with the general format specifier. However, if this value is not successfully parsed to the same numeric value, it is formatted like this: Double uses 17-bit precision, and single uses 9-bit precision.
Although a precision specifier can exist, it is ignored. When this specifier is used, the round-trip process takes precedence over precision.
Console.WriteLine ("(25.00)". ToString (' R '): "+ (25.00). ToString ("R", Culinfo)); 25
Console.WriteLine ("(25.00)". ToString (' R1 '): "+ (25.00). ToString ("R1", Culinfo)); 25
Console.WriteLine ("(25.00)". ToString (' R2 '): "+ (25.00). ToString ("R2", Culinfo)); 25


Console.readkey ();
}
}
}

Time to format
DateTime dt = DateTime.Now;  String date; date = dt.   ToString ("D", datetimeformatinfo.invariantinfo); Response.Write (Date + "﹤/br﹥");//07/22/2009 date = dt.   ToString ("G", datetimeformatinfo.invariantinfo); Response.Write (Date + "﹤/br﹥"),//07/22/2009 14:54:37 date = dt.  ToString ("R", Datetimeformatinfo.invariantinfo); Response.Write (Date + "﹤/br﹥"),//wed, 14:54:37 GMT date = dt.  ToString ("T", datetimeformatinfo.invariantinfo); Response.Write (Date + "﹤/br﹥");//14:54:37 date = dt.  ToString ("U", datetimeformatinfo.invariantinfo); Response.Write (Date + "﹤/br﹥"),//2009-07-22 14:54:37z date = dt.  ToString ("dd-mm-yyyy", datetimeformatinfo.invariantinfo); Response.Write (Date + "﹤/br﹥");//22-07-2009

[C #]. ToString () formatting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.