only for RuBoard - do not distribute or recompile Previous Section Next Section

C.1 Picture Format Specifiers

Table C-2 lists the valid picture format specifiers supported by the ToStringmethod 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.

Table C-2. Picture format specifiers

Specifier

String result

0

Zero placeholder

#

Digit placeholder

.

Decimal point

,

Group separator or multiplier

%

Percent notation

E0, E+0, E-0, e0 e+0, e-0

Exponent notation

\

Literal character quote

'xx'"xx" 

Literal string quote

;

Section separator

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);           // %
  }
}
only for RuBoard - do not distribute or recompile Previous Section Next Section