C# Format Specifiers—Picture Format Specifiers
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Tutorials |
| C# Tutorials |
|
| © 2006 O'Reilly & Assoc., Inc. |
Picture Format Specifiers
Table B-2 lists the valid picture format specifiers supported by the Format method on the predefined numeric types (see the documentation for System.IFormattable in the .NET SDK).
Table B-2. Picture Format Specifiers
| Specifier | String Result |
| 0 | Zero placeholder |
| # | Digit placeholder |
| . | Decimal point |
| , | Group separator or multiplier |
| % | Percent notation |
| E+0, E-0 e+0, e-0 | Exponent notation |
| \ | Literal character quote |
| 'xx'"xx" | Literal string quote |
| ; | Section separator |
This example uses 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); // 123 Console.WriteLine("{0:#0;(#0);<zero>}", i); // 123 Console.WriteLine("{0:#%}", i); // 12300% i = -123; Console.WriteLine("{0:#0}", i); // -123 Console.WriteLine("{0:#0;(#0)}", i); // (123) Console.WriteLine("{0:#0;(#0);<zero>}", i); // (123) Console.WriteLine("{0:#%}", i); // -12300% i = 0; Console.WriteLine("{0:#0}", i); // 0 Console.WriteLine("{0:#0;(#0)}", i); // 0 Console.WriteLine("{0:#0;(#0);<zero>}", i); // <zero> Console.WriteLine("{0:#%}", i); // % } }
The following example 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); // % } }
|

