With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

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}", ...`

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required