Appendix C. Format Specifiers

Table 3.1 lists the numeric format specifiers supported by the Format method on the predefined numeric types (see Chapter 3).

Table C-1. Numeric Format Specifiers

Specifier

String Result

Datatype

C[ n ]

$XX,XX.XX
($XX,XXX.XX)

Currency

D[ n ]

[-]XXXXXXX

Decimal

E[ n ] or e[ n ]

[-]X.XXXXXXE+xxx
[-]X.XXXXXXe+xxx
[-]X.XXXXXXE-xxx
[-]X.XXXXXXe-xxx

Exponent

F[ n ]

[-]XXXXXXX.XX

Fixed point

G[ n ]

General or scientific

General

N[ n ]

[-]XX,XXX.XX

Number

X[ n ] or x[ n ]

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:X}", i); // 9FBF1
    Console.WriteLine("{0:x}", i); // 9fbf1
  }
}

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: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: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: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
    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
    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
  }
}

Picture Format Specifiers

Table 3.2 lists the valid picture format specifiers supported by the Format method on t he predefined numeric types (see the documentation for System.IFormattable in the .NET SDK).

Table C-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

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);        // 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 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);           // %
  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.93.175