BlackWaspTM
C# Programming
.NET 1.1+

C# Number to String Conversion (2)

The nineteenth part of the C# Fundamentals tutorial continues the examination of conversion between string data and numeric data. This article considers the reverse of the previous part, this time transforming numeric data into formatted strings.

Available Format Specifiers

The full list of format specifiers and the effect of the precision specifier for each is as follows:

SpecifierDescriptionEffect of Precision Specifier
C or cFormats the number as a monetary value including the correct number of decimal places and the appropriate currency symbol for the user's local settingSpecifies a fixed number of decimal places.
D or dFormats integers only as simple whole numbers.Specifies the minimum number of digits. Leading zeroes are added if required.
EFormats numbers using exponential notation. The resultant string includes an upper case letter 'E'.Specifies a fixed number of decimal places for the mantissa. If omitted, six decimal places are used.
eFormats numbers using exponential notation. The resultant string includes a lower case letter 'e'.Specifies a fixed number of decimal places for the mantissa. If omitted, six decimal places are used.
F or fFormats numbers using fixed-point notation.Specifies a fixed number of decimal places.
G or gFormats numbers using either exponential or fixed-point notation, whichever produces the shortest string. The actual results vary according to the data type being converted and whether a precision specifier is used.See 'e', 'E' and 'F or f'.
N or nFormats numbers using fixed-point notation with thousands separators.Specifies a fixed number of decimal places.
P or pFormats numbers using percentage notation. For example, 0.25 is formatted as 25%.Specifies a fixed number of decimal places.
R or rFormats numbers using Round-Trip Format. This is a special format that ensures that the string generated can be converted back to the original number using Parse methods.Unused.
XConverts numbers into a string representation of their hexadecimal value. The digits 0 to 9 and A to F are used.Specifies the minimum number of digits. Leading zeroes are added if required.
xConverts numbers into a string representation of their hexadecimal value. The digits 0 to 9 and a to f are used.Specifies the minimum number of digits. Leading zeroes are added if required.

Using Picture Formats for Custom Formatting

The standard format specifiers are useful in most situations. However, there may be circumstances where the format specifiers do not provide the desired results. In these cases you can use a picture format. A picture format is a string containing special characters that affect the final conversion. There are a number of control characters; each will be considered in the following sections.

Zero Placeholder

A zero placeholder digit (0) in a picture format indicates that a digit is required in the resultant converted string. If there is a digit in the original number at the placeholder position it is copied to the final output. If there is no digit at this position a zero is used instead. This allows the generation of leading zeroes. If the integer part of a number being converted is longer than the number of placeholder digits the extra digits will be included to avoid truncation. Digits following the decimal point may be rounded however.

int small = 12;
int large = 123456;
float tiny = 1.234F;

Console.WriteLine(small.ToString("0000"));         // Outputs "0012"
Console.WriteLine(large.ToString("0000"));         // Outputs "123456"
Console.WriteLine(tiny.ToString("0000"));          // Outputs "0001"
12 November 2006