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

Appendix C. Format Specifiers

Table C-1 lists the numeric format specifiers supported by the ToString method on the predefined numeric types (see Chapter 3).

Table C-1. Numeric format specifiers

Specifier

String result

Data type

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

P[n]

[-]XX,XXX.XX %

Percent

R

Round-trip format

Floating point

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