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.
| 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
|
| 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.
| 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
|
| 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.
| 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
|
| 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.
| 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
|
| 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.
| 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
|
| 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.
| Dim value As Double = .086
Console.WriteLine(value.ToString("#0.##%", CultureInfo.InvariantCulture))
' Displays 8.6%
|
| 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.
| Dim value As Double = .00354
Dim perMilleFmt As String = "#0.## " & ChrW(&h2030)
Console.WriteLine(value.ToString(perMilleFmt, CultureInfo.InvariantCulture))
' Displays 3.54 ‰
|
| 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.
| 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
|
| 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.
| 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)
|
| 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.
|