.NET Framework 4 Custom Numeric Format Strings Updated: March 2011 The following table describes the custom numeric format specifiers and displays sample output produced by each format specifier. See the Notes section for additional information about using custom numeric format strings, and the Example section for a comprehensive illustration of their use. Format specifier | Name | Description | Examples |
|---|
"0" | Zero placeholder | Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string. More information: The "0" Custom Specifier. | 1234.5678 ("00000") -> 01235 0.45678 ("0.00", en-US) -> 0.46 0.45678 ("0.00", fr-FR) -> 0,46 | "#" | Digit placeholder | Replaces the pound sign with the corresponding digit if one is present; otherwise, no digit appears in the result string. More information: The "#" Custom Specifier. | 1234.5678 ("#####") -> 1235 0.45678 ("#.##", en-US) -> .46 0.45678 ("#.##", fr-FR) -> ,46 | "." | Decimal point | Determines the location of the decimal separator in the result string. More information: The "." Custom Specifier. | 0.45678 ("0.00", en-US) -> 0.46 0.45678 ("0.00", fr-FR) -> 0,46 | "," | Group separator and number scaling | Serves as both a group separator and a number scaling specifier. As a group separator, it inserts a localized group separator character between each group. As a number scaling specifier, it divides a number by 1000 for each comma specified. More information: The "," Custom Specifier. | Group separator specifier: 2147483647 ("##,#", en-US) -> 2,147,483,647 2147483647 ("##,#", es-ES) -> 2.147.483.647 Scaling specifier: 2147483647 ("#,#,,", en-US) -> 2,147 2147483647 ("#,#,,", es-ES) -> 2.147 | "%" | Percentage placeholder | Multiplies a number by 100 and inserts a localized percentage symbol in the result string. More information: The "%" Custom Specifier. | 0.3697 ("%#0.00", en-US) -> %36.97 0.3697 ("%#0.00", el-GR) -> %36,97 0.3697 ("##.0 %", en-US) -> 37.0 % 0.3697 ("##.0 %", el-GR) -> 37,0 % | "‰" | Per mille placeholder | Multiplies a number by 1000 and inserts a localized per mille symbol in the result string. More information: The "‰" Custom Specifier. | 0.03697 ("#0.00‰", en-US) -> 36.97‰ 0.03697 ("#0.00‰", ru-RU) -> 36,97‰ | "E0" "E+0" "E-0" "e0" "e+0" "e-0" | Exponential notation | If followed by at least one 0 (zero), formats the result using exponential notation. The case of "E" or "e" indicates the case of the exponent symbol in the result string. The number of zeros following the "E" or "e" character determines the minimum number of digits in the exponent. A plus sign (+) indicates that a sign character always precedes the exponent. A minus sign (-) indicates that a sign character precedes only negative exponents. More information: The "E" and "e" Custom Specifiers. | 987654 ("#0.0e0") -> 98.8e4 1503.92311 ("0.0##e+00") -> 1.504e+03 1.8901385E-16 ("0.0e+00") -> 1.9e-16 | \ | Escape character | Causes the next character to be interpreted as a literal rather than as a custom format specifier. More information: The "\" Escape Character. | 987654 ("\###00\#") -> #987654# | 'string' "string" | Literal string delimiter | Indicates that the enclosed characters should be copied to the result string unchanged. | 68 ("# ' degrees'") -> 68 degrees 68 ("#' degrees'") -> 68 degrees | ; | Section separator | Defines sections with separate format strings for positive, negative, and zero numbers. More information: The ";" Section Separator. | 12.345 ("#0.0#;(#0.0#);-\0-") -> 12.35 0 ("#0.0#;(#0.0#);-\0-") -> -0- -12.345 ("#0.0#;(#0.0#);-\0-") -> (12.35) 12.345 ("#0.0#;(#0.0#)") -> 12.35 0 ("#0.0#;(#0.0#)") -> 0.0 -12.345 ("#0.0#;(#0.0#)") -> (12.35) | Other | All other characters | The character is copied to the result string unchanged. | 68 ("# °") -> 68 ° |
The following sections provide detailed information about each of the custom numeric format specifiers.

The "0" Custom Specifier
The "0" custom format specifier serves as a zero-placeholder symbol. If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string. The position of the leftmost zero before the decimal point and the rightmost zero 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 that are formatted by 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("0,0", CultureInfo.InvariantCulture))
' Displays 1,234,567,890
Console.WriteLine(value.ToString("0,0",
CultureInfo.CreateSpecificCulture("el-GR")))
' Displays 1.234.567.890
value = 1234567890.123456
Console.WriteLine(value.ToString("0,0.0", CultureInfo.InvariantCulture))
' Displays 1,234,567,890.1
value = 1234.567890
Console.WriteLine(value.ToString("0,0.00", CultureInfo.InvariantCulture))
' Displays 1,234.57
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("0,0", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
Console.WriteLine(value.ToString("0,0",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 1.234.567.890
value = 1234567890.123456;
Console.WriteLine(value.ToString("0,0.0", CultureInfo.InvariantCulture));
// Displays 1,234,567,890.1
value = 1234.567890;
Console.WriteLine(value.ToString("0,0.00", CultureInfo.InvariantCulture));
// Displays 1,234.57
Back to table

The "#" Custom Specifier
The "#" custom format specifier serves as a digit-placeholder symbol. If the value that is being formatted has a digit in the position where the pound sign appears in the format string, 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 a zero that is not a significant digit, even if zero is the only digit in the string. It will display zero only if it is a significant digit in the number that is 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 that are formatted by 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
Back to table

The "." Custom Specifier
The "." custom format specifier inserts a localized decimal separator into the result string. The first period in the format string determines the location of the decimal separator in the formatted value; any additional periods are ignored. The character that is used as the decimal separator in the result string is not always a period; it is determined by the NumberDecimalSeparator property of the NumberFormatInfo object that controls formatting. The following example uses the "." 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
Back to table

The "," Custom Specifier
The "," character serves as both a group separator and a number scaling specifier. Group separator: If one or more commas are 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 commas are specified immediately to the left of the explicit or implicit decimal point, the number to be formatted is divided by 1000 for each comma. For example, if the string "0,," is used to format the number 100 million, the output is "100".
You can use group 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 group 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
Back to table

The "%" Custom Specifier
A percent sign (%) in a format string causes a number to be multiplied by 100 before it is formatted. The localized percent symbol is inserted in the number at the location where the % appears in the format string. The percent character used is defined by the PercentSymbol property of the current NumberFormatInfo object. The following example defines several custom format strings that include the "%" custom specifier.
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%
Back to table

The "‰" Custom Specifier
A per mille character (‰ or \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 "‰" custom specifier.
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‰
Back to table

The "E" and "e" Custom Specifiers
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 zero, the number is formatted by using scientific notation with an "E" or "e" inserted between the number and the exponent. The number of zeros following the scientific notation indicator determines the minimum number of digits to output for the exponent. The "E+" and "e+" formats indicate that a plus sign or minus sign should always precede the exponent. The "E", "E-", "e", or "e-" formats indicate that a sign character should precede only 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
Back to table

The "\" Escape Character
The "#", "0", ".", ",", "%", and "‰" symbols in a format string are interpreted as format specifiers rather than as literal characters. Depending on their position in a custom format string, the uppercase and lowercase "E" as well as the + and - symbols may also be interpreted as format specifiers. To prevent a character from being interpreted as a format specifier, you can precede it with a backslash, which is the escape character. The escape character signifies that the following character is a character literal that should be included in the result string unchanged. To include a backslash in a result string, you must escape it with another backslash (\\). Note |
|---|
Some compilers, such as the C++ and C# compilers, may also interpret a single backslash character as an escape character. To ensure that a string is interpreted correctly when formatting, you can use the verbatim string literal character (the @ character) before the string in C#, or add another backslash character before each backslash in C# and C++. The following C# example illustrates both approaches. |
The following example uses the escape character to prevent the formatting operation from interpreting the "#", "0", and "\" characters as either escape characters or format specifiers. The C# examples uses an additional backslash to ensure that a backslash is interpreted as a literal character.
Dim value As Integer = 123
Console.WriteLine(value.ToString("\#\#\# ##0 dollars and \0\0 cents \#\#\#"))
' Displays ### 123 dollars and 00 cents ###
Console.WriteLine(value.ToString("\\\\\\ ##0 dollars and \0\0 cents \\\\\\"))
' Displays \\\ 123 dollars and 00 cents \\\
int value = 123;
Console.WriteLine(value.ToString("\\#\\#\\# ##0 dollars and \\0\\0 cents \\#\\#\\#"));
// Displays ### 123 dollars and 00 cents ###
Console.WriteLine(value.ToString(@"\#\#\# ##0 dollars and \0\0 cents \#\#\#"));
// Displays ### 123 dollars and 00 cents ###
Console.WriteLine(value.ToString("\\\\\\\\\\\\ ##0 dollars and \\0\\0 cents \\\\\\\\\\\\"));
// Displays \\\ 123 dollars and 00 cents \\\
Console.WriteLine(value.ToString(@"\\\\\\ ##0 dollars and \0\0 cents \\\\\\"));
// Displays \\\ 123 dollars and 00 cents \\\
Back to table

The ";" Section Separator
The semicolon (;) is a conditional format specifier that applies different formatting to a number depending on whether its 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, 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, 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 example uses the ";" format specifier to format positive, negative, and zero numbers differently.
Dim posValue As Double = 1234
Dim negValue As Double = -1234
Dim zeroValue As Double = 0
Dim fmt2 As String = "##;(##)"
Dim fmt3 As String = "##;(##);**Zero**"
Console.WriteLine(posValue.ToString(fmt2)) ' Displays 1234
Console.WriteLine(negValue.ToString(fmt2)) ' Displays (1234)
Console.WriteLine(zeroValue.ToString(fmt3)) ' Displays **Zero**
double posValue = 1234;
double negValue = -1234;
double zeroValue = 0;
string fmt2 = "##;(##)";
string fmt3 = "##;(##);**Zero**";
Console.WriteLine(posValue.ToString(fmt2)); // Displays 1234
Console.WriteLine(negValue.ToString(fmt2)); // Displays (1234)
Console.WriteLine(zeroValue.ToString(fmt3)); // Displays **Zero**
Back to table

Notes
Floating-Point Infinities and NaNControl Panel SettingsThe 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 that use different settings generate different result strings. In addition, if you use the CultureInfo..::.CultureInfo(String) constructor to instantiate a new CultureInfo object that represents the same culture as the current system culture, any customizations established by the Regional and Language Options item in Control Panel will be applied to the new CultureInfo object. You can use the CultureInfo..::.CultureInfo(String, Boolean) constructor to create a CultureInfo object that does not reflect a system's customizations. Rounding and Fixed-Point Format StringsFor 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. Back to table

Example
The following example demonstrates two custom numeric format strings. In both cases, the digit placeholder (#) displays the numeric data, and all other characters are copied to the result string.
Dim number1 As Double = 1234567890
Dim value1 As String = number1.ToString("(###) ###-####")
Console.WriteLine(value1)
Dim number2 As Integer = 42
Dim value2 As String = number2.ToString("My Number = #")
Console.WriteLine(value2)
' The example displays the following output:
' (123) 456-7890
' My Number = 42
double number1 = 1234567890;
string value1 = number1.ToString("(###) ###-####");
Console.WriteLine(value1);
int number2 = 42;
string value2 = number2.ToString("My Number = #");
Console.WriteLine(value2);
// The example displays the following output:
// (123) 456-7890
// My Number = 42
Back to table

See Also

Change History
Date | History | Reason |
|---|
March 2011
| Added a link to the Format Utility. |
Information enhancement.
|
|
.NET Framework 4 カスタム数値書式指定文字列 更新 : 2011 年 3 月 次の表に、カスタム数値書式指定子の説明および書式指定子ごとのサンプル出力を示します。 カスタム数値書式指定文字列の使用方法については、「メモ」を参照してください。それらを使用する包括的な例については、「例」を参照してください。 書式指定子 | 名前 | 説明 | 例 |
|---|
"0" | ゼロ プレースホルダー | 対応する数字でゼロを置き換えます。置き換えが行われるのは、対応する数字が存在する場合です。それ以外の場合は、結果の文字列にはゼロが表示されます。 詳細については、「"0" カスタム指定子」を参照してください。 | 1234.5678 ("00000") -> 01235 0.45678 ("0.00", en-US) -> 0.46 0.45678 ("0.00", fr-FR) -> 0,46 | "#" | 桁プレースホルダー | 対応する数字でシャープ記号を置き換えます。置き換えが行われるのは、対応する数字が存在する場合です。それ以外の場合は、結果の文字列に数字は表示されません。 詳細については、「"#" カスタム指定子」を参照してください。 | 1234.5678 ("#####") -> 1235 0.45678 ("#.##", en-US) -> .46 0.45678 ("#.##", fr-FR) -> ,46 | "." | 小数点 | 結果の文字列の小数点位置を決定します。 詳細については、「"." カスタム指定子」を参照してください。 | 0.45678 ("0.00", en-US) -> 0.46 0.45678 ("0.00", fr-FR) -> 0,46 | "," | 桁区切り記号および数値の位取り | 桁区切り記号および数値位取り指定子の両方として機能します。 桁区切り記号としては、各グループの間に、ローカライズされた桁区切り記号文字を挿入します。 数値位取り指定子としては、指定されたコンマごとに、数値を 1000 で除算します。 詳細については、「"," カスタム指定子」を参照してください。 | 桁区切り記号: 2147483647 ("##,#", en-US) -> 2,147,483,647 2147483647 ("##,#", es-ES) -> 2.147.483.647 位取り指定子: 2147483647 ("#,#,,", en-US) -> 2,147 2147483647 ("#,#,,", es-ES) -> 2.147 | "%" | パーセント プレースホルダー | 数値に 100 を乗算し、結果の文字列にローカライズされたパーセント記号を挿入します。 詳細については、「"%" カスタム指定子」を参照してください。 | 0.3697 ("%#0.00", en-US) -> %36.97 0.3697 ("%#0.00", el-GR) -> %36,97 0.3697 ("##.0 %", en-US) -> 37.0 % 0.3697 ("##.0 %", el-GR) -> 37,0 % | "‰" | パーミル プレースホルダー | 数値に 1000 を乗算し、結果の文字列にローカライズされたパーミル記号を挿入します。 詳細については、「"‰" カスタム指定子」を参照してください。 | 0.03697 ("#0.00‰", en-US) -> 36.97‰ 0.03697 ("#0.00‰", ru-RU) -> 36,97‰ | "E0" "E+0" "E-0" "e0" "e+0" "e-0" | 指数表記 | 後に 0 (ゼロ) が 1 つ以上続く場合に、指数表記を使用して結果の書式を設定します。 大文字 "E" と小文字 "e" は、結果の文字列の指数記号を大文字にするか小文字にするかを示します。 "E" 文字または "e" 文字の後に続くゼロの数によって、指数部の最小桁数が決まります。 正符号 (+) は、符号文字が指数部の前に常に挿入されることを示します。 負符号 (-) は、指数部が負の値の場合にだけその前に符号文字が挿入されることを示します。 詳細については、「"E" カスタム指定子と "e" カスタム指定子」を参照してください。 | 987654 ("#0.0e0") -> 98.8e4 1503.92311 ("0.0##e+00") -> 1.504e+03 1.8901385E-16 ("0.0e+00") -> 1.9e-16 | \ | エスケープ文字 | この文字の次の文字はカスタム書式指定子ではなくリテラルとして解釈されます。 詳細については、「"\" エスケープ文字」を参照してください。 | 987654 ("\###00\#") -> #987654# | 'string' "string" | リテラル文字列区切り記号 | 囲まれた文字列が結果の文字列にそのままコピーされることを示します。 | 68 ("# ' degrees'") -> 68 degrees 68 ("#' degrees'") -> 68 degrees | ; | セクション区切り記号 | 正の数値、負の数値、およびゼロの数値に対して、別々の書式指定文字列を使用してセクションを定義します。 詳細については、「";" セクション区切り記号」を参照してください。 | 12.345 ("#0.0#;(#0.0#);-\0-") -> 12.35 0 ("#0.0#;(#0.0#);-\0-") -> -0- -12.345 ("#0.0#;(#0.0#);-\0-") -> (12.35) 12.345 ("#0.0#;(#0.0#)") -> 12.35 0 ("#0.0#;(#0.0#)") -> 0.0 -12.345 ("#0.0#;(#0.0#)") -> (12.35) | その他 | 上記以外のすべての文字 | 文字が結果の文字列にそのままコピーされます。 | 68 ("# °") -> 68 ° |
以降では、それぞれのカスタム数値書式指定子について詳しく説明します。

"0" カスタム指定子
"0" カスタム書式指定子は、ゼロ プレースホルダー記号として機能します。 書式が設定される値で、書式指定文字列のゼロに対応する位置に数字がある場合には、この数字が結果の文字列にコピーされます。それ以外の場合は、結果の文字列にゼロが表示されます。 整数部の左端のゼロの位置と、小数部の右端のゼロの位置によって、常に結果の文字列に示される桁数が決まります。 指定子が "00" の場合、値は一の位で丸められ、小数点以下のゼロは常に切り捨てられます。 たとえば、"00" を指定して 34.5 を書式設定すると、結果の値は 35 になります。 次の例では、ゼロ プレースホルダーを含むカスタム書式指定文字列を使って書式設定された複数の値を表示します。
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("0,0", CultureInfo.InvariantCulture))
' Displays 1,234,567,890
Console.WriteLine(value.ToString("0,0",
CultureInfo.CreateSpecificCulture("el-GR")))
' Displays 1.234.567.890
value = 1234567890.123456
Console.WriteLine(value.ToString("0,0.0", CultureInfo.InvariantCulture))
' Displays 1,234,567,890.1
value = 1234.567890
Console.WriteLine(value.ToString("0,0.00", CultureInfo.InvariantCulture))
' Displays 1,234.57
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("0,0", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
Console.WriteLine(value.ToString("0,0",
CultureInfo.CreateSpecificCulture("el-GR")));
// Displays 1.234.567.890
value = 1234567890.123456;
Console.WriteLine(value.ToString("0,0.0", CultureInfo.InvariantCulture));
// Displays 1,234,567,890.1
value = 1234.567890;
Console.WriteLine(value.ToString("0,0.00", CultureInfo.InvariantCulture));
// Displays 1,234.57
表のトップへ

"#" カスタム指定子
"#" カスタム書式指定子は、桁プレースホルダー記号として機能します。 書式が設定される値で、書式指定文字列のシャープ記号に対応する位置に数字がある場合には、この数字が結果の文字列にコピーされます。 それ以外の場合は、結果の文字列のこの位置には何も格納されません。 この指定子では、文字列の唯一の桁の値がゼロであっても、この桁が有効桁でない場合には、ゼロは表示されません。 表示される数値の有効桁である場合にのみゼロが表示されます。 書式指定文字列が "##" の場合は、値は一の位で丸められ、小数点以下のゼロは常に切り捨てられます。 たとえば、"##" を指定して 34.5 を書式設定すると、結果の値は 35 になります。 次の例では、桁プレースホルダーを含むカスタム書式指定文字列を使って書式設定された複数の値を表示します。
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
表のトップへ

"." カスタム指定子
"." カスタム書式指定子は、ローカライズされた小数点を結果の文字列に挿入します。 書式指定文字列の 1 番目のピリオドによって、書式設定後の値での小数点の位置が決定します。指定されている他のピリオドは無視されます。 結果の文字列の中で小数点として使用される文字は、ピリオドであるとは限りません。書式設定を制御する NumberFormatInfo オブジェクトの NumberDecimalSeparator プロパティによって決定されます。 次の例では、結果として得られるさまざまな文字列の小数点位置を、"." 書式指定子を使って定義しています。
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
表のトップへ

"," カスタム指定子
"," 文字は、桁区切り記号および数値位取り指定子の両方として機能します。 桁区切り記号: 数値の整数部の桁を書式設定する 2 つの桁プレースホルダー (0 または #) の間に 1 つ以上のコンマが指定されている場合は、出力の整数部分で各数値グループの間に桁区切り記号文字が挿入されます。 現在の NumberFormatInfo オブジェクトの NumberGroupSeparator プロパティと NumberGroupSizes プロパティによって、桁区切り記号として使用される文字および各数値グループのサイズが決まります。 たとえば、文字列 "#,#" およびインバリアント カルチャを使用して数値 1000 が書式設定される場合は、出力が "1,000" となります。 数値位取り指定子: 明示的または暗黙的な小数点のすぐ左側に 1 つ以上のコンマが指定されている場合は、コンマごとに書式設定対象の数値が 1000 で除算されます。 たとえば、"0,," 文字列を使用して数値 1 億が書式設定された場合、出力は "100" となります。
桁区切り記号および数値位取り指定子は、同じ書式指定文字列で使用できます。 たとえば、"#,0,," 文字列およびインバリアント カルチャを使用して 10 億の数値を書式設定した場合、出力は "1,000" となります。 桁区切り記号としてコンマを使用する例を次に示します。
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
数値の位取り指定子としてコンマを使用する例を次に示します。
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
表のトップへ

"%" カスタム指定子
書式指定文字列にパーセント記号 (%) があると、書式設定前に数値に 100 が乗算されます。 数値では、書式指定文字列の % に対応する位置にローカライズされたパーセント記号が挿入されます。 使用されるパーセント記号は、現在の NumberFormatInfo オブジェクトの PercentSymbol プロパティによって定義されます。 次の例では、"%" カスタム指定子を含むさまざまなカスタム書式指定文字列を定義します。
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%
表のトップへ

"‰" カスタム指定子
書式指定文字列にパーミル文字 (‰ または \u2030) があると、書式設定前に数値に 1000 が乗算されます。 返される文字列では、書式指定文字列の ‰ に対応する位置に適切なパーミル記号が挿入されます。 使用されるパーミル文字は、カルチャ固有の書式設定情報を指定するオブジェクトの NumberFormatInfo..::.PerMilleSymbol プロパティによって定義されます。 次の例では、"‰" カスタム指定子を含むカスタム書式指定文字列を定義します。
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‰
表のトップへ

"E" カスタム指定子と "e" カスタム指定子
書式指定文字列に "E"、"E+"、"E-"、"e"、"e+"、または "e-" が含まれており、これらの文字の直後にゼロが 1 つ以上続く場合には、指数表記を使用して数値の書式が設定されます。また、数値と指数の間に "E" または "e" が挿入されます。 指数表記インジケーターの後に続くゼロの数によって、出力の指数部の最小桁数が決まります。 書式 "E+" または "e+" は、正符号または負符号が指数部の前に常に挿入されることを示します。 書式 "E"、"E-"、"e"、または "e-" は、指数部が負の値の場合にだけその前に符号文字が挿入されることを示します。 次の例では、指数表記の指定子を使用して、さまざまな数値の書式を設定します。
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
表のトップへ

"\" エスケープ文字
書式指定文字列内の "#"、"0"、"."、","、"%"、"‰" の各記号は、リテラル文字ではなく書式指定子として解釈されます。 カスタム書式指定文字列内での位置によっては、大文字および小文字の "E"、および + 記号と - 記号も書式指定子として解釈されます。 文字が書式指定子として解釈されないようにするには、その文字の前に、エスケープ文字の円記号を付けます。 エスケープ文字は、その後に続く文字が、そのまま結果の文字列に含める必要がある文字リテラルであることを示します。 結果の文字列に円記号を含める場合は、円記号をもう 1 つ付けて、円記号自体をエスケープする必要があります (\\)。 メモ |
|---|
C++ コンパイラや C# コンパイラなど、一部のコンパイラでは、同様に、1 つの円記号がエスケープ文字として解釈されることがあります。 書式設定時に文字列が正しく解釈されるようにするには、C# では、逐語的文字列リテラル文字 (@ 文字) を文字列の前に使用します。また、C# および C++ では、円記号の前にもう 1 つ円記号を付ける方法もあります。 両方の方法を次の C# の例に示します。 |
次の例では、エスケープ文字を使用して、書式設定操作で "#"、"0"、"\" の各文字がエスケープ文字としても書式指定子としても解釈されないようにします。 この C# の例では、円記号をもう 1 つ付けて、円記号がリテラル文字として解釈されるようにしています。
Dim value As Integer = 123
Console.WriteLine(value.ToString("\#\#\# ##0 dollars and \0\0 cents \#\#\#"))
' Displays ### 123 dollars and 00 cents ###
Console.WriteLine(value.ToString("\\\\\\ ##0 dollars and \0\0 cents \\\\\\"))
' Displays \\\ 123 dollars and 00 cents \\\
int value = 123;
Console.WriteLine(value.ToString("\\#\\#\\# ##0 dollars and \\0\\0 cents \\#\\#\\#"));
// Displays ### 123 dollars and 00 cents ###
Console.WriteLine(value.ToString(@"\#\#\# ##0 dollars and \0\0 cents \#\#\#"));
// Displays ### 123 dollars and 00 cents ###
Console.WriteLine(value.ToString("\\\\\\\\\\\\ ##0 dollars and \\0\\0 cents \\\\\\\\\\\\"));
// Displays \\\ 123 dollars and 00 cents \\\
Console.WriteLine(value.ToString(@"\\\\\\ ##0 dollars and \0\0 cents \\\\\\"));
// Displays \\\ 123 dollars and 00 cents \\\
表のトップへ

";" セクション区切り記号
セミコロン (;) は、値が正、負、ゼロのいずれであるかに応じて異なる書式設定を数値に適用する条件付き書式指定子です。 このように数値の内容によって適用する書式を変更するには、カスタム書式指定文字列に、セミコロンで区切ったセクションを最大 3 つまで作成します。 これらのセクションの説明を次に示します。 セクションの数 | 説明 |
|---|
1 つ | 書式指定文字列はすべての値に適用されます。 | 2 つ | 最初のセクションが正の値とゼロに適用され、2 番目のセクションが負の値に適用されます。 書式設定対象の数値が負の数値であるが、2 番目のセクションの書式指定に従って丸めた結果ゼロになる場合には、1 番目のセクションの書式に従ってこのゼロが書式設定されます。 | 3 つ | 最初のセクションが正の値、2 番目のセクションが負の値、3 番目のセクションがゼロに適用されます。 ゼロ以外の値すべてに 1 番目のセクションが適用される場合には、2 番目のセクションが空になる (セミコロンの間に何も表示されない) ことがあります。 書式設定対象の数値がゼロ以外の負の数値であるが、1 番目または 2 番目のセクションの書式に従って丸めた結果ゼロになる場合には、3 番目のセクションの書式に従ってこのゼロが書式設定されます。 |
セクション区切り記号は、最後の値が書式設定されるときに、数値に関連付けられた既存の書式設定をすべて無視します。 たとえば、セクション区切り記号を使用する場合、負の値はマイナス記号を付けずに表示されます。 最終的に書式設定された値にマイナス記号を付ける場合は、カスタム書式指定子の中に明示的にマイナス記号を含める必要があります。 次の例では、";" 書式指定子を使用して、正数、負数、ゼロの各部分に対し、それぞれ異なる書式を設定します。
Dim posValue As Double = 1234
Dim negValue As Double = -1234
Dim zeroValue As Double = 0
Dim fmt2 As String = "##;(##)"
Dim fmt3 As String = "##;(##);**Zero**"
Console.WriteLine(posValue.ToString(fmt2)) ' Displays 1234
Console.WriteLine(negValue.ToString(fmt2)) ' Displays (1234)
Console.WriteLine(zeroValue.ToString(fmt3)) ' Displays **Zero**
double posValue = 1234;
double negValue = -1234;
double zeroValue = 0;
string fmt2 = "##;(##)";
string fmt3 = "##;(##);**Zero**";
Console.WriteLine(posValue.ToString(fmt2)); // Displays 1234
Console.WriteLine(negValue.ToString(fmt2)); // Displays (1234)
Console.WriteLine(zeroValue.ToString(fmt3)); // Displays **Zero**
表のトップへ

メモ
浮動小数点の無限大値と NaN (非数) 値コントロール パネルの設定丸めと固定小数点の書式指定文字列固定小数点の書式指定文字列 (つまり指数表記の書式指定文字を含まない書式指定文字列) の場合は、小数点以下の桁数が小数点の右側にある桁プレースホルダーの数と同じである数値に丸められます。 書式指定文字列に小数点が含まれていない場合には、最も近い整数に丸められます。 数値の桁数が、整数部の桁プレースホルダーの数よりも大きい場合には、桁プレースホルダーに収まらない桁が、結果の文字列の 1 番目の桁プレースホルダーの直前にコピーされます。 表のトップへ

例
2 つのカスタム数値書式指定文字列の例を次に示します。 どちらの場合も、桁プレースホルダー (#) によって数値データが表示され、それ以外の文字はすべて結果の文字列にコピーされます。
Dim number1 As Double = 1234567890
Dim value1 As String = number1.ToString("(###) ###-####")
Console.WriteLine(value1)
Dim number2 As Integer = 42
Dim value2 As String = number2.ToString("My Number = #")
Console.WriteLine(value2)
' The example displays the following output:
' (123) 456-7890
' My Number = 42
double number1 = 1234567890;
string value1 = number1.ToString("(###) ###-####");
Console.WriteLine(value1);
int number2 = 42;
string value2 = number2.ToString("My Number = #");
Console.WriteLine(value2);
// The example displays the following output:
// (123) 456-7890
// My Number = 42
表のトップへ

参照

履歴の変更
日付 | 履歴 | 理由 |
|---|
2011 年 3 月
| 書式指定ユーティリティへのリンクを追加。 |
情報の拡充
|
|