Table C-1 lists the numeric format specifiers supported by the
ToString
method on the predefined
numeric types (see Chapter
3).
Specifier | String result | Data type |
|
| Currency |
|
| Decimal |
|
| Exponent |
|
| Fixed point |
| General or scientific | General |
|
| Number |
|
| Percent |
R | Round-trip format | Floating point |
| Hex representation | Hex |
This is an example that uses numeric format specifiers without precision specifiers:
using System; class TestDefaultFormats { static void Main( ) { 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:P}", i); // 65,432,100.00 % Console.WriteLine("{0:P}", .42); // 42.00 % Console.WriteLine("{0:X}", i); // 9FBF1 Console.WriteLine("{0:x}", i); // 9fbf1 // Round-trip conversions string s1 = Math.PI.ToString("G"); Console.WriteLine(s1); // 3.14159265358979 string s2 = Math.PI.ToString("R"); Console.WriteLine(s2); // 3.1415926535897931 Console.WriteLine(Math.PI == Double.Parse(s1)); // False Console.WriteLine(Math.PI == Double.Parse(s2)); // True } }
This is an example that uses numeric format specifiers with
precision specifiers on a variety of int
values:
using System; class TestIntegerFormats { static void Main( ) { int 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:P6}", i); // 12,300.000000 % Console.WriteLine("{0:X6}", i); // 00007B 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:P6}", i); // -12,300.000000 % Console.WriteLine("{0:X6}", i); // FFFF85 i = 0; Console.WriteLine("{0:C6}", i); // $0.000000 Console.WriteLine("{0:D6}", i); // 000000 Console.WriteLine("{0:E6}", i); // 0.000000E+000 Console.WriteLine("{0:G6}", i); // 0 Console.WriteLine("{0:N6}", i); // 0.000000 Console.WriteLine("{0:P6}", i); // 0.000000 % Console.WriteLine("{0:X6}", i); // 000000 } }
Here’s an example that uses numeric format specifiers with precision
specifiers on a variety of double
values:
using System; class TestDoubleFormats { static void Main( ) { double d = 1.23; Console.WriteLine("{0:C6}", d); // $1.230000 Console.WriteLine("{0:E6}", d); // 1.230000E+000 Console.WriteLine("{0:G6}", d); // 1.23 Console.WriteLine("{0:N6}", d); // 1.230000 Console.WriteLine("{0:P6}", d); // 123.000000 % d = -1.23; Console.WriteLine("{0:C6}", d); // ($1.230000) Console.WriteLine("{0:E6}", d); // -1.230000E+000 Console.WriteLine("{0:G6}", d); // -1.23 Console.WriteLine("{0:N6}", d); // -1.230000 Console.WriteLine("{0:P6}", d); // -123.000000 % d = 0; Console.WriteLine("{0:C6}", d); // $0.000000 Console.WriteLine("{0:E6}", d); // 0.000000E+000 Console.WriteLine("{0:G6}", d); // 0 Console.WriteLine("{0:N6}", d); // 0.000000 Console.WriteLine("{0:P6}", d); // 0.000000 % } }
Table C-2 lists
the valid picture format specifiers supported by the ToString
method on t he predefined numeric types (see the documentation for
System.IFormattable
in the .NET SDK).
The picture format can include up to three sections separated by a
semicolon. With one section, the format specifier is applied to all
values. With two sections, the first format specifier applies to zero
and positive values, and the second applies to negative values. With
three sections, the first specifier applies to positive values, the
second applies to negative values, and the third applies to zero.
Here’s an example using picture format specifiers on some int
values:
using System; class TestIntegerCustomFormats { static void Main( ) { int i = 123; Console.WriteLine("{0:#0}", i); // 123 Console.WriteLine("{0:#0;(#0)}", i); // Two sections: 123 Console.WriteLine("{0:#0;(#0);<zero>}", i); // Three sections: 123 Console.WriteLine("{0:#%}", i); // 12300% i = -123; Console.WriteLine("{0:#0}", i); // -123 Console.WriteLine("{0:#0;(#0)}", i); // Two sections: (123) Console.WriteLine("{0:#0;(#0);<zero>}", i); // Three sections: (123) Console.WriteLine("{0:#%}", i); // -12300% i = 0; Console.WriteLine("{0:#0}", i); // 0 Console.WriteLine("{0:#0;(#0)}", i); // Two sections: 0 Console.WriteLine("{0:#0;(#0);<zero>}", i); // Three sections: <zero> Console.WriteLine("{0:#%}", i); // % } }
The following is an example that uses these picture format
specifiers on a variety of double
values:
using System; class TestDoubleCustomFormats { static void Main( ) { double d = 1.23; Console.WriteLine("{0:#.000E+00}", d); // 1.230E+00 Console.WriteLine( "{0:#.000E+00;(#.000E+00)}", d); // 1.230E+00 Console.WriteLine( "{0:#.000E+00;(#.000E+00);<zero>}", d); // 1.230E+00 Console.WriteLine("{0:#%}", d); // 123% d = -1.23; Console.WriteLine("{0:#.000E+00}", d); // -1.230E+00 Console.WriteLine( "{0:#.000E+00;(#.000E+00)}", d); // (1.230E+00) Console.WriteLine( "{0:#.000E+00;(#.000E+00);<zero>}", d); // (1.230E+00) Console.WriteLine("{0:#%}", d); // -123% d = 0; Console.WriteLine("{0:#.000E+00}", d); // 0.000E+01 Console.WriteLine( "{0:#.000E+00;(#.000E+00)}", d); // 0.000E+01 Console.WriteLine( "{0:#.000E+00;(#.000E+00);<zero>}", d); // <zero> Console.WriteLine("{0:#%}", d); // % } }
Table C-3 lists
the valid format specifiers supported by the Format
method on the DateTime
type (see System.IFormattable
).
Here’s an example that uses these custom format specifiers on a
DateTime
value:
using System; class TestDateTimeFormats { static void Main( ) { DateTime dt = new DateTime(2000, 10, 11, 15, 32, 14); // Prints "10/11/2000 3:32:14 PM" Console.WriteLine(dt.ToString( )); // Prints "10/11/2000 3:32:14 PM" Console.WriteLine("{0}", dt); // Prints "10/11/2000" Console.WriteLine("{0:d}", dt); // Prints "Wednesday, October 11, 2000" Console.WriteLine("{0:D}", dt); // Prints "Wednesday, October 11, 2000 3:32 PM" Console.WriteLine("{0:f}", dt); // Prints "Wednesday, October 11, 2000 3:32:14 PM" Console.WriteLine("{0:F}", dt); // Prints "10/11/2000 3:32 PM" Console.WriteLine("{0:g}", dt); // Prints "10/11/2000 3:32:14 PM" Console.WriteLine("{0:G}", dt); // Prints "October 11" Console.WriteLine("{0:m}", dt); // Prints "October 11" Console.WriteLine("{0:M}", dt); // Prints "Wed, 11 Oct 2000 15:32:14 GMT" Console.WriteLine("{0:r}", dt); // Prints "Wed, 11 Oct 2000 15:32:14 GMT" Console.WriteLine("{0:R}", dt); // Prints "3:32 PM" Console.WriteLine("{0:t}", dt); // Prints "3:32:14 PM" Console.WriteLine("{0:T}", dt); // Prints "2000-10-11T15:32:14" Console.WriteLine("{0:s}", dt); // Prints "2000-10-11 15:32:14Z" Console.WriteLine("{0:u}", dt); // Prints "Wednesday, October 11, 2000 7:32:14 PM" Console.WriteLine("{0:U}", dt); // Prints "October, 2000" Console.WriteLine("{0:y}", dt); // Prints "October, 2000" Console.WriteLine("{0:Y}", dt); // Prints "Wednesday the 11 day of October in the year 2000" Console.WriteLine( "{0:dddd 'the' d 'day of' MMMM 'in the year' yyyy}", dt); } }