[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Use standard numeric formats in C#

standard numeric formats

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
NameFormatResult
CurrencyC or c($12,345.68)
ExponentialE or e-1.234568E+004
Fixed PointF or f-12345.68
GeneralG or g-12345.6789
NumberN or n-12,345.68
PercentP or p-1,234,567.89 %
Round-tripR or r-12345.6789
 
Integer Formats
NameFormatResult
DecimalD or d-123456789
HexadecimalH or hF8A432EB

The example program displays a similar table in a ListView control.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.