Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Formatting Types
 Custom Numeric Format Strings
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Developer's Guide
Custom Numeric Format Strings

Updated: July 2008

A custom numeric format string, which you create and consists of one or more custom numeric format specifiers, defines how numeric data is formatted. A custom numeric format string is equivalently defined as any string that is not a standard numeric format string.

The following table describes the custom numeric format specifiers. For more information, see the notes that follow the table.

Format specifier

Name

Description

0

Zero placeholder

If the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the result string; otherwise, a '0' appears in the result string. The position of the leftmost '0' before the decimal point and the rightmost '0' after the decimal point determines the range of digits that are always present in the result string.

The "00" specifier causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "00" would result in the value 35.

The following example displays several values formatted using custom format strings that include zero placeholders.

Visual Basic
Dim value As Double

value = 123
Console.WriteLine(value.ToString("00000"))
' Displays 00123

value = 1.2
Console.Writeline(value.ToString("0.00", CultureInfo.InvariantCulture))
' Displays 1.20
Console.WriteLine(value.ToString("00.00", CultureInfo.InvariantCulture))
' Displays 01.20
Console.WriteLine(value.ToString("00.00", _
                  CultureInfo.CreateSpecificCulture("da-DK")))
' Displays 01,20

value = .56
Console.WriteLine(value.ToString("0.0", CultureInfo.InvariantCulture))
' Displays 0.6

value = 1234567890
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture))    
' Displays 1,234,567,890      
Console.WriteLine(value.ToString("#,#", _
                  CultureInfo.CreateSpecificCulture("el-GR")))    
' Displays 1.234.567.890      

C#
double value;

value = 123;
Console.WriteLine(value.ToString("00000"));
// Displays 00123

value = 1.2;
Console.WriteLine(value.ToString("0.00", CultureInfo.InvariantCulture));
// Displays 1.20
Console.WriteLine(value.ToString("00.00", CultureInfo.InvariantCulture));
// Displays 01.20
Console.WriteLine(value.ToString("00.00", 
                  CultureInfo.CreateSpecificCulture("da-DK")));
// Displays 01,20

value = .56;
Console.WriteLine(value.ToString("0.0", CultureInfo.InvariantCulture));
// Displays 0.6

value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));    
// Displays 1,234,567,890      
Console.WriteLine(value.ToString("#,#", 
                  CultureInfo.CreateSpecificCulture("el-GR")));    
// Displays 1.234.567.890      

#

Digit placeholder

If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string.

Note that this specifier never displays the '0' character if it is not a significant digit, even if '0' is the only digit in the string. It will display the '0' character if it is a significant digit in the number being displayed.

The "##" format string causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "##" would result in the value 35.

The following example displays several values formatted using custom format strings that include digit placeholders.

Visual Basic
      Dim value As Double

      value = 1.2
      Console.WriteLine(value.ToString("#.##", CultureInfo.InvariantCulture))
      ' Displays 1.2

      value = 123
      Console.WriteLine(value.ToString("#####"))
      ' Displays 123

      value = 123456
      Console.WriteLine(value.ToString("[##-##-##]"))      
       ' Displays [12-34-56]

      value = 1234567890
      Console.WriteLine(value.ToString("#"))
      ' Displays 1234567890
      Console.WriteLine(value.ToString("(###) ###-####"))
      ' Displays (123) 456-7890

C#
      double value;

      value = 1.2;
      Console.WriteLine(value.ToString("#.##", CultureInfo.InvariantCulture));
      // Displays 1.2

      value = 123;
      Console.WriteLine(value.ToString("#####"));
      // Displays 123

      value = 123456;
      Console.WriteLine(value.ToString("[##-##-##]"));      
       // Displays [12-34-56]

      value = 1234567890;
      Console.WriteLine(value.ToString("#"));
      // Displays 1234567890
      Console.WriteLine(value.ToString("(###) ###-####"));
      // Displays (123) 456-7890

.

Decimal point

The first '.' character in the format string determines the location of the decimal separator in the formatted value; any additional '.' characters are ignored.

The actual character used as the decimal separator in the result string is determined by the NumberDecimalSeparator property of the NumberFormatInfo object that controls formatting.

The following example uses the decimal point format specifier to define the location of the decimal point in several result strings.

Visual Basic
      Dim value As Double

      value = 1.2
      Console.Writeline(value.ToString("0.00", CultureInfo.InvariantCulture))
      ' Displays 1.20
      Console.WriteLine(value.ToString("00.00", CultureInfo.InvariantCulture))
      ' Displays 01.20
      Console.WriteLine(value.ToString("00.00", _
                        CultureInfo.CreateSpecificCulture("da-DK")))
      ' Displays 01,20

      value = .086
      Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture)) 
      ' Displays 8.6%

      value = 86000
      Console.WriteLine(value.ToString("0.###E+0", CultureInfo.InvariantCulture))
       ' Displays 8.6E+4

C#
      double value;

      value = 1.2;
      Console.WriteLine(value.ToString("0.00", CultureInfo.InvariantCulture));
      // Displays 1.20
      Console.WriteLine(value.ToString("00.00", CultureInfo.InvariantCulture));
      // Displays 01.20
      Console.WriteLine(value.ToString("00.00", 
                        CultureInfo.CreateSpecificCulture("da-DK")));
      // Displays 01,20

      value = .086;
      Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture)); 
      // Displays 8.6%

      value = 86000;
      Console.WriteLine(value.ToString("0.###E+0", CultureInfo.InvariantCulture));
       // Displays 8.6E+4

,

Thousand separator and number scaling

The ',' character serves as both a thousand separator specifier and a number scaling specifier.

Thousand separator specifier: If one or more ',' characters is specified between two digit placeholders (0 or #) that format the integral digits of a number, a group separator character is inserted between each number group in the integral part of the output.

The NumberGroupSeparator and NumberGroupSizes properties of the current NumberFormatInfo object determine the character used as the number group separator and the size of each number group. For example, if the string "#,#" and the invariant culture are used to format the number 1000, the output is "1,000".

Number scaling specifier: If one or more ',' characters is specified immediately to the left of the explicit or implicit decimal point, the number to be formatted is divided by 1000 each time a number scaling specifier occurs. For example, if the string "0,," is used to format the number 100 million, the output is "100".

You can use thousand separator and number scaling specifiers in the same format string. For example, if the string "#,0,," and the invariant culture are used to format the number one billion, the output is "1,000".

The following example illustrates the use of the comma as a thousand separator.

Visual Basic
Dim value As Double = 1234567890
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture))
' Displays 1,234,567,890      
Console.WriteLine(value.ToString("#,##0,,", CultureInfo.InvariantCulture))
' Displays 1,235          

C#
double value = 1234567890;
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));
// Displays 1,234,567,890      
Console.WriteLine(value.ToString("#,##0,,", CultureInfo.InvariantCulture));
// Displays 1,235          

The following example illustrates the use of the comma as a specifier for number scaling.

Visual Basic
      Dim value As Double = 1234567890
      Console.WriteLine(value.ToString("#,,", CultureInfo.InvariantCulture))    
      ' Displays 1235   
      Console.WriteLine(value.ToString("#,,,", CultureInfo.InvariantCulture))
       ' Displays 1  
      Console.WriteLine(value.ToString("#,##0,,", CultureInfo.InvariantCulture))       
       ' Displays 1,235

C#
      double value = 1234567890;
      Console.WriteLine(value.ToString("#,,", CultureInfo.InvariantCulture));    
      // Displays 1235   
      Console.WriteLine(value.ToString("#,,,", CultureInfo.InvariantCulture));
       // Displays 1  
      Console.WriteLine(value.ToString("#,##0,,", CultureInfo.InvariantCulture));       
       // Displays 1,235

%

Percentage placeholder

The presence of a '%' character in a format string causes a number to be multiplied by 100 before it is formatted. The appropriate symbol is inserted in the number itself at the location where the '%' appears in the format string. The percent character used is dependent on the current NumberFormatInfo class.

The following example defines several custom format strings that include the percentage placeholder.

Visual Basic
Dim value As Double = .086
Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture))
' Displays 8.6%      

C#
double value = .086;
Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture));
// Displays 8.6%      

Per mille placeholder

The presence of a '‰' character (\u2030) in a format string causes a number to be multiplied by 1000 before it is formatted. The appropriate per mille symbol is inserted in the returned string at the location where the '‰' symbol appears in the format string. The per mille character used is defined by the NumberFormatInfo..::.PerMilleSymbol property of the object that provides culture-specific formatting information.

The following example defines a custom format string that includes the per mille placeholder.

Visual Basic
Dim value As Double = .00354
Dim perMilleFmt As String = "#0.## " & ChrW(&h2030)
Console.WriteLine(value.ToString(perMilleFmt, CultureInfo.InvariantCulture))
' Displays 3.54 ‰      

C#
double value = .00354;
string perMilleFmt = "#0.## " + '\u2030';
Console.WriteLine(value.ToString(perMilleFmt, CultureInfo.InvariantCulture));
// Displays 3.54‰      

E0

E+0

E-0

e0

e+0

e-0

Scientific notation

If any of the strings "E", "E+", "E-", "e", "e+", or "e-" are present in the format string and are followed immediately by at least one '0' character, then the number is formatted using scientific notation with an 'E' or 'e' inserted between the number and the exponent. The number of '0' characters following the scientific notation indicator determines the minimum number of digits to output for the exponent. The "E+" and "e+" formats indicate that a sign character (plus or minus) should always precede the exponent. The "E", "E-", "e", or "e-" formats indicate that a sign character should only precede negative exponents.

The following example formats several numeric values using the specifiers for scientific notation.

Visual Basic
Dim value As Double = 86000
Console.WriteLine(value.ToString("0.###E+0", CultureInfo.InvariantCulture))
' Displays 8.6E+4
Console.WriteLine(value.ToString("0.###E+000", CultureInfo.InvariantCulture))
' Displays 8.6E+004
Console.WriteLine(value.ToString("0.###E-000", CultureInfo.InvariantCulture))
' Displays 8.6E004

C#
double value = 86000;
Console.WriteLine(value.ToString("0.###E+0", CultureInfo.InvariantCulture));
// Displays 8.6E+4
Console.WriteLine(value.ToString("0.###E+000", CultureInfo.InvariantCulture));
// Displays 8.6E+004
Console.WriteLine(value.ToString("0.###E-000", CultureInfo.InvariantCulture));
// Displays 8.6E004

\

Escape character

In C# and C++, the backslash character causes the next character in the format string to be interpreted as an escape sequence. It is used with traditional formatting sequences like '\n' (new line).

In some languages, the escape character itself must be preceded by an escape character when used as a literal. Otherwise, the compiler interprets the character as an escape sequence. Use the string "\\" to display '\'.

Note that this escape character is not supported in Visual Basic; however, ControlChars provides the same functionality.

'ABC'

"ABC"

Literal string

Characters enclosed in single or double quotes are copied to the result string, and do not affect formatting.

;

Section separator

The ';' character is used to separate sections for positive, negative, and zero numbers in the format string. If there are two sections in the custom format string, the leftmost section defines the formatting of positive and zero numbers, while the rightmost section defines the formatting of negative numbers. If there are three sections, the leftmost section defines the formatting of positive numbers, the middle section defines the formatting of negative numbers, and the rightmost section defines the formatting of zero numbers.

The following example uses the format specifier for the section separator to format positive, negative, and zero numbers differently.

Visual Basic
Dim posValue As Double = 1234
Dim negValue As Double = -1234
Dim fmt As String = "##;(##)"
Console.WriteLine(posValue.ToString(fmt))    ' Displays 1234
Console.WriteLine(negValue.ToString(fmt))    ' Displays (1234)

C#
double posValue = 1234;
double negValue = -1234;
string fmt = "##;(##)";
Console.WriteLine(posValue.ToString(fmt));    // Displays 1234
Console.WriteLine(negValue.ToString(fmt));    // Displays (1234)

Other

All other characters

Any other character is copied to the result string, and does not affect formatting.

Floating-Point Infinities and NaN

Note that regardless of the format string, if the value of a Single or Double floating-point type is positive infinity, negative infinity, or Not a Number (NaN), the formatted string is the value of the respective PositiveInfinitySymbol, NegativeInfinitySymbol, or NaNSymbol property specified by the currently applicable NumberFormatInfo object.

Control Panel Settings

The settings in the Regional and Language Options item in Control Panel influence the result string produced by a formatting operation. Those settings are used to initialize the NumberFormatInfo object associated with the current thread culture, and the current thread culture provides values used to govern formatting. Computers using different settings will generate different result strings.

Rounding and Fixed-Point Format Strings

Note that for fixed-point format strings (that is, format strings that do not contain scientific notation format characters), numbers are rounded to as many decimal places as there are digit placeholders to the right of the decimal point. If the format string does not contain a decimal point, the number is rounded to the nearest integer. If the number has more digits than there are digit placeholders to the left of the decimal point, the extra digits are copied to the result string immediately before the first digit placeholder.

Section Separators and Conditional Formatting

Different formatting can be applied to a string based on whether the value is positive, negative, or zero. To produce this behavior, a custom format string can contain up to three sections separated by semicolons. These sections are described in the following table.

Number of sections

Description

One section

The format string applies to all values.

Two sections

The first section applies to positive values and zeros, and the second section applies to negative values.

If the number to be formatted is negative, but becomes zero after rounding according to the format in the second section, then the resulting zero is formatted according to the first section.

Three sections

The first section applies to positive values, the second section applies to negative values, and the third section applies to zeros.

The second section can be left empty (by having nothing between the semicolons), in which case the first section applies to all nonzero values.

If the number to be formatted is nonzero, but becomes zero after rounding according to the format in the first or second section, then the resulting zero is formatted according to the third section.

Section separators ignore any preexisting formatting associated with a number when the final value is formatted. For example, negative values are always displayed without a minus sign when section separators are used. If you want the final formatted value to have a minus sign, you should explicitly include the minus sign as part of the custom format specifier.

The following code fragments illustrate how section separators can be used to produce formatted strings.

Visual Basic
Dim MyPos As Double = 19.95
Dim MyNeg As Double = -19.95
Dim MyZero As Double = 0

' In the U.S. English culture, MyString has the value: $19.95.
Dim MyString As String = MyPos.ToString("$#,##0.00;($#,##0.00);Zero")

' In the U.S. English culture, MyString has the value: ($19.95).
' The minus sign is omitted by default.
MyString = MyNeg.ToString("$#,##0.00;($#,##0.00);Zero")

' In the U.S. English culture, MyString has the value: Zero.
MyString = MyZero.ToString("$#,##0.00;($#,##0.00);Zero")
C#
double MyPos = 19.95, MyNeg = -19.95, MyZero = 0.0;

// In the U.S. English culture, MyString has the value: $19.95.
string MyString = MyPos.ToString("$#,##0.00;($#,##0.00);Zero");

// In the U.S. English culture, MyString has the value: ($19.95).
// The minus sign is omitted by default.
MyString = MyNeg.ToString("$#,##0.00;($#,##0.00);Zero");

// In the U.S. English culture, MyString has the value: Zero.
MyString = MyZero.ToString("$#,##0.00;($#,##0.00);Zero");

Two Custom Format Examples

The following code fragments demonstrate custom numeric formatting. In both cases the digit placeholder (#) in the custom format string displays the numeric data, and all other characters are copied to the output.

Visual Basic
Dim myDouble As Double = 1234567890
Dim myString As String = myDouble.ToString( "(###) ### - ####" )
' The value of myString is "(123) 456 – 7890".

Dim MyInt As Integer = 42
MyString = MyInt.ToString( "My Number = #" )
' In the U.S. English culture, MyString has the value: 
' "My Number = 42".
C#
Double myDouble = 1234567890;
String myString = myDouble.ToString( "(###) ### - ####" );
// The value of myString is "(123) 456 – 7890".

int  MyInt = 42;
MyString = MyInt.ToString( "My Number = #" );
// In the U.S. English culture, MyString has the value: 
// "My Number = 42".

Date

History

Reason

July 2008

Added per mille placeholder.

Content bug fix.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker