This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Number to String Conversion

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.

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"

Decimal Point Placeholder

The second special character is the decimal point placeholder. This can be combined with the zero placeholder to identify the number of digits required both before and after the decimal point. Although the decimal point placeholder character is always a full stop or period character (.), the actual character in the resultant string is dependant upon the user's local settings. The examples assume that the user has a UK configuration:

int small = 12;
double large = 123456.789;
float tiny = 1.234F;

Console.WriteLine(small.ToString("00.00"));        // Outputs "12.00"
Console.WriteLine(large.ToString("00.00"));        // Outputs "123456.79"
Console.WriteLine(tiny.ToString("00.00"));         // Outputs "01.23"

Digit Placeholder

The digit placeholder (#) is similar to the zero placeholder. It defines the position of a digit within the resultant formatted string and causes rounding after the decimal point. However, the digit placeholder does not cause leading or trailing zeroes to be added to a number where the original numeric value has no digit in the appropriate position.

int small = 12;
double large = 123456.789;
float tiny = 1.234F;

Console.WriteLine(small.ToString("##.##"));        // Outputs "12"
Console.WriteLine(large.ToString("##.##"));        // Outputs "123456.79"
Console.WriteLine(tiny.ToString("##.##"));         // Outputs "1.23"

The digit placeholder has a side effect when converted a zero value to a string. As the placeholder will not cause the creation of either leading or trailing zeroes, when converting a zero value the resultant string is empty.

Console.WriteLine(0.ToString("##.##"));            // Outputs an empty string

Thousands Separator Placeholder

The thousands separator placeholder is represented as a comma (,). If a comma is included in the picture format to the left of any decimal point and between zero or digit placeholders, thousands separators are inserted into the result. Although the thousands placeholder character is always a comma, the actual character in the resultant string is dependant upon the user's local settings.

int small = 12;
double large = 123456.789;
float tiny = 1.234F;

Console.WriteLine(small.ToString("#,#.00"));       // Outputs "12.00"
Console.WriteLine(large.ToString("#,#.##"));       // Outputs "123,456.79"
Console.WriteLine(tiny.ToString("#,#.##"));        // Outputs "1.23"

Number Scaling Character

The number scaling character is represented as a comma (,). To differentiate between the thousands separator, the number scaling character must appear directly to the left of a decimal point or at the end of the picture format if no decimal point placeholder is present. Each comma included divides the number by one thousand before applying formatting.

int miles = 123456789;

Console.WriteLine(miles.ToString("#,#,"));         // Outputs "123,457"
Console.WriteLine(miles.ToString("#,#,,.###"));    // Outputs "123.457"
Console.WriteLine(miles.ToString("#,#,,"));        // Outputs "123"

Percentage Placeholder

The percentage placeholder (%) is used when displaying percentage values. The placeholder specifies the position in the resultant string where the percentage symbol should appear and multiplies the value by one hundred. The actual symbol inserted is base upon the user's local settings.

int small = 12;
double large = 123456.789;
float tiny = 0.0015F;

Console.WriteLine(small.ToString("#,#.#%"));       // Outputs "1,200%"
Console.WriteLine(large.ToString("#,#.#%"));       // Outputs "12,345,678.9%"
Console.WriteLine(tiny.ToString("#,#.#%"));        // Outputs ".2%"

Custom Exponential Notation

Picture formats can be used to generate custom scientific or exponential notation. Several characters are used to determine the exact formatting used. The first character that is required is an upper case "E" or a lower case "e". This indicates that exponential notation is to be used. It is followed by one or more zeroes to indicate the minimum number of digits for the exponent. The mantissa formatting is controlled by placeholder characters that appear before the 'E'.

int small = 12;
double large = 123456.789;
float tiny = 0.0015F;

Console.WriteLine(small.ToString("0.0E0"));        // Outputs "1.2E1"
Console.WriteLine(large.ToString("0.00e00"));      // Outputs "1.23e05"
Console.WriteLine(tiny.ToString("0.00E00"));       // Outputs "1.50E-03"

The exponential notation can be modified by including a plus or minus symbol after the letter. A "+" indicates that the sign for the exponent value should always be included. A minus "-" indicates that the sign character should only appear for negative exponent values.

int small = 12;
double large = 123456.789;
float tiny = 0.0015F;

Console.WriteLine(small.ToString("0.0E-0"));       // Outputs "1.2E1"
Console.WriteLine(large.ToString("0.00e+00"));     // Outputs "1.23e+05"
Console.WriteLine(tiny.ToString("0.00E+00"));      // Outputs "1.50E-03"
12 November 2006