Information Technology Reference
In-Depth Information
The Format Component
The format component specifies the form that the numeric representation should take. For
example, should it be represented as currency, in decimal format, in hexadecimal format, or in
fixed point notation?
The format component has two parts, as shown in Figure 23-2:
￿The format specifier is a single alphabetic character, from a set of nine built-in character
formats. The character can be uppercase or lowercase. The case is significant for some
specifiers, but insignificant for others.
￿The precision specifier is optional, and consists of one or two digits. Its actual meaning
depends on the format specifier.
Figure 23-2. Standard format specifier string
The following code shows an example of the syntax of the format string component.
Index -- use 0th item in the list.
Console.WriteLine("{0: F4 }", 12.345678);
Format component -- fixed-point, four decimal places.
Some examples of different format strings are shown in the following code.
double MyDouble = 12.345678;
Console.WriteLine("{0,-10:G} -- General", MyDouble);
Console.WriteLine("{0,-10} -- Default, same as General", MyDouble);
Console.WriteLine("{0,-10:F4} -- Fixed Point, 4 dec places", MyDouble);
Console.WriteLine("{0,-10:C} -- Currency", MyDouble);
Console.WriteLine("{0,-10:E3} -- Sci. Notation, 3 dec places", MyDouble);
Console.WriteLine("{0,-10:x} -- Hexadecimal integer", 1194719 );
This code produces the following output:
12.345678 -- General
12.345678 -- Default, same as General
12.3457 -- Fixed Point, 4 dec places
$12.35 -- Currency
1.235E+001 -- Sci. Notation, 3 dec places
123adf -- Hexadecimal integer
Search WWH ::




Custom Search