Format Specifiers
Table 1-17
lists the numericformat specifiers
supported by the Format
method on the predefined
numeric types.
Table 1-17. Numeric format specifiers
Specifier |
String result |
Datatype |
---|---|---|
|
|
Currency |
|
|
Decimal |
|
|
Exponent |
|
|
Fixed point |
|
General or scientific |
General |
|
|
Number |
|
Hex representation |
Hex |
This example uses a variety of numeric format specifiers:
using System; class TestDefaultFormats { static void Main( ) { // no precision specifiers int i = 654321; Console.WriteLine("{0:C}", i); // $654,321.00 Console.WriteLine("{0:D}", i); // 654321 Console.WriteLine("{0:E}", i); // 6.543210E+005 Console.WriteLine("{0:F}", i); // 654321.00 Console.WriteLine("{0:G}", i); // 654321 Console.WriteLine("{0:N}", i); // 654,321.00 Console.WriteLine("{0:X}", i); // 9FBF1 Console.WriteLine("{0:x}", i); // 9fbf1 // use precision specifiers i = 123; Console.WriteLine("{0:C6}", i); // $123.000000 Console.WriteLine("{0:D6}", i); // 000123 Console.WriteLine("{0:E6}", i); // 1.230000E+002 Console.WriteLine("{0:G6}", i); // 123 Console.WriteLine("{0:N6}", i); // 123.000000 Console.WriteLine("{0:X6}", i); // 00007B // use a double value double d = 1.23; Console.WriteLine("{0:C6}", d); // $1.230000 Console.WriteLine("{0:E6}", d); // 1.230000E+000 Console.WriteLine("{0:G6}", ...
Get C# Language Pocket Reference now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.