Title: Use standard numeric formats in C#
You can use numeric formatting characters to display numbers in particular formats. These formats work with a variable's ToString method as well as with String.Format. For example, the code value.ToString("C") returns the variable value formatted as currency.
These formats are locale-aware so they produce a result that is appropriate for the user's computer. For example, if the "C" format uses a dollar sign $ in the united States, a pound symbol £ in England, and the euro symbol € in Germany. Similarly the different formats use appropriate decimal symbols and thousands separators.
The following table shows the standard numeric format characters and their results in the United States.
Floating Point Formats |
Name | Format | Result |
Currency | C or c | ($12,345.68) |
Exponential | E or e | -1.234568E+004 |
Fixed Point | F or f | -12345.68 |
General | G or g | -12345.6789 |
Number | N or n | -12,345.68 |
Percent | P or p | -1,234,567.89 % |
Round-trip | R or r | -12345.6789 |
Integer Formats |
Name | Format | Result |
Decimal | D or d | -123456789 |
Hexadecimal | H or h | F8A432EB |
The example program displays a similar table in a ListView control.
Download the example to experiment with it and to see additional details.
|