System.IFormatProvider

This interface provides important functionality, allowing a class that implements the methods defined in this interface to customize formatted output. The IFormatProvider interface provides custom formatting for ToString. Some custom format codes already exist for numeric data types, date and time formatting, and enumerators. You can also provide your own formatting by inheriting from the IFormatProvider interface and implementing the methods specified.

You might want to look in the online documentation for “Custom Numeric Format Strings” because the documentation there is much more detailed than this appendix is able to cover. Listing B.8 shows an example of formatting a double (iformatprovider.cs).

Listing B.8. Numeric IFormatProvider Example
double d = 123.0;
Console.WriteLine("{0}  		-> {1}  		-> {2} ", d, @"#####", d.ToString("#####"));
Console.WriteLine("{0}  		-> {1}  		-> {2} ", d, @"00000", d.ToString("00000"));
d = 1234567890.0;
Console.WriteLine("{0}  	-> {1}  	-> {2} ", d, @"(###) ### - ####", d.ToString("(###)
 ### - ####"));
d = 1.2;
Console.WriteLine("{0}  		-> {1}  		-> {2} ", d, @"#.##", d.ToString("#.##"));
Console.WriteLine("{0}  		-> {1}  		-> {2} ", d, @"0.00", d.ToString("0.00"));
Console.WriteLine("{0}  		-> {1}  		-> {2} ", d, @"00.00", d.ToString("00.00"));
d = 1234567890.0;
. . .

Listing B.9 shows the output of the code in Listing B.8.

Listing B.9. Numeric IFormatProvider Example Output
123             -> #####                -> 123
123             -> 00000                -> 00123
1234567890      -> (###) ### - ####     -> (123) 456 - 7890
1.2             -> #.##                 -> 1.2
1.2             -> 0.00                 -> 1.20
1.2             -> 00.00                -> 01.20
1234567890      -> #,#                  -> 1,234,567,890
1234567890      -> #,,                  -> 1235
1234567890      -> #,,,                 -> 1
1234567890      -> #,##0,,              -> 1,235
0.086           -> #0.##%               -> 8.6%
86000           -> 0.###E+0             -> 8.6E+4
86000           -> 0.###E+000           -> 8.6E+004
86000           -> 0.###E-000           -> 8.6E004
123456          -> [##-##-##]           -> [12-34-56]
1234            -> ##;(##)              -> 1234
-1234           -> ##;(##)              -> (1234)

The DateTime structure also has some custom format strings. Listing B.10 shows some examples (iformatprovider.cs).

Listing B.10. DateTime IFormatProvider Example
DateTime now = DateTime.Now;
Console.WriteLine("{0}  	-> {1}  		-> {2} ", now, @"d, M", now.ToString("d, M"));
Console.WriteLine("{0}  	-> {1}  		-> {2} ", now, @"d MMMM", now.ToString("d MMMM"));
Console.WriteLine("{0}  	-> {1}  	-> {2} ", now, @"dddd MMMM yy gg", now.ToString("dddd
 MMMM yy gg"));
Console.WriteLine("{0}  	-> {1}  		-> {2} ", now, @"h , m: s", now.ToString("h , m: s"));
. . .

Listing B.11 shows the output from Listing B.10. Your output, of course, will differ due to time differences, time zone differences, and culture settings.

Listing B.11. DateTime IFormatProvider Example Output
10/3/2001 1:20:11 PM    -> d, M                 -> 3, 10
10/3/2001 1:20:11 PM    -> d MMMM               -> 3 October
10/3/2001 1:20:11 PM    -> dddd MMMM yy gg      -> Wednesday October 01 A.D.
10/3/2001 1:20:11 PM    -> h , m: s             -> 1 , 20: 11
10/3/2001 1:20:11 PM    -> hh,mm:ss             -> 01,20:11
10/3/2001 1:20:11 PM    -> HH-mm-ss-tt          -> 13-20-11-PM
10/3/2001 1:20:11 PM    -> hh:mm, GMT z        -> 01:20, GMT -5
10/3/2001 1:20:11 PM    -> hh:mm, GMT zzz      -> 01:20, GMT -05:00

The enum type implements an IFormatProvider interface. Listing B.12 shows the usage of the codes that enum supports (iformatprovider.cs).

Listing B.12. Enum IFormatProvider Example
enum Letters
{
    A,
    B,
    C,
    D,
    E,
    F,
    G,
    H,
. . .
Letters c = Letters.R;
Console.WriteLine("{0}  	-> {1}  	-> {2} ", c, @"G", c.ToString("G"));
Console.WriteLine("{0}  	-> {1}  	-> {2} ", c, @"F", c.ToString("F"));
Console.WriteLine("{0}  	-> {1}  	-> {2} ", c, @"D", c.ToString("D"));
Console.WriteLine("{0}  	-> {1}  	-> {2} ", c, @"X", c.ToString("X"));

The output from this listing is shown in Listing B.13.

Listing B.13. Enum IFormatProvider Example Output
R       -> G    -> R
R       -> F    -> R
R       -> D    -> 17
R       -> X    -> 00000011

..................Content has been hidden....................

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