Information Technology Reference
In-Depth Information
Standard Numeric Format Specifiers
The Windows Control Panel applet, Regional and Language Options, can affect the resulting
formats of some of the specifiers. For example, the currency symbol of the country or region
specified will be used by the currency format specifier.
Table 23-2 summarizes the nine standard numeric format specifiers. The first column lists
the name of the specifier followed by the specifier characters. If the specifier characters have
different output depending on their case, they are marked case sensitive .
Table 23-2. Standard Numeric Format Specifiers
Name and Characters
Meaning
Currency
C , c
Formats the value as a currency, using a currency symbol.
Precision specifier: The number of decimal places.
Sample: Console.WriteLine("{0 :C}", 12.5);
Output: $12.50
Decimal
D , d
A string of decimal digits, with a negative sign, if appropriate. Can be used
only with integral types.
Precision specifier: The minimum number of digits to use in the output
string. If the number has fewer digits, it will be padded with 0s on the left.
Sample: Console.WriteLine("{0 :D4}", 12);
Output: 0012
Scientific
E , e
Case sensitive
Scientific notation with a mantissa and an exponent. The exponent is
preceded by the letter E . The E will be the same case as the specifier.
Precision specifier: The number of decimal places.
Sample: Console.WriteLine("{0 :e4}", 12.3456789);
Output: 1.2346e+001
Fixed-point
F , f
A string of decimal digits with a decimal point. Can also include a nega-
tive sign, if appropriate.
Precision specifier: The number of decimal places.
Sample: Console.WriteLine("{0 :F4}", 12.3456789);
Output: 12.3457
General
G , g
A compact fixed-point representation or a scientific notation representa-
tion, depending on the value. This is the default, if no specifier is listed.
Precision specifier: Depends on the value.
Sample: Console.WriteLine("{0 :G4}", 12.3456789);
Output: 12.35
Number
N , n
Similar to fixed-point representation, but includes separators between
each group of three digits, starting at the decimal point and going left.
Precision specifier: The number of decimal places.
Sample: Console.WriteLine("{0 :N2}", 12345678.54321);
Output: 12,345,678.54
Continued
Search WWH ::




Custom Search