# 標準數值格式字串Standard Numeric Format Strings

• A 是稱為「格式規範」的單一字母字元。A is a single alphabetic character called the format specifier. 任何包含一個以上字母字元 (包含泛空白字元 (White Space)) 的數值格式字串都會解譯為自訂數值格式字串。Any numeric format string that contains more than one alphabetic character, including white space, is interpreted as a custom numeric format string. 如需詳細資訊，請參閱自訂數值格式字串For more information, see Custom Numeric Format Strings.

• xx 是稱為「精確度規範」的選用性整數。xx is an optional integer called the precision specifier. 精確度規範的範圍從 0 到 99，而且會影響結果內的位數。The precision specifier ranges from 0 to 99 and affects the number of digits in the result. 請注意，精確度規範可控制數字字串表示法中的位數。Note that the precision specifier controls the number of digits in the string representation of a number. 它不會捨入數字本身。It does not round the number itself. 若要執行捨入運算，請使用 Math.CeilingMath.FloorMath.Round 方法。To perform a rounding operation, use the Math.Ceiling, Math.Floor, or Math.Round method.

當「有效位數規範」控制結果字串中小數點後的位數，結果字串會反映已四捨五入為最接近無限精確度之可代表結果的數字。When precision specifier controls the number of fractional digits in the result string, the result string reflects a number that is rounded to a representable result nearest to the infinitely precise result. 若有兩個相等的近似可代表結果：If there are two equally near representable results:

• 在 .NET Framework 與 .NET Core (最高版本 .NET Core 2.0) 上，執行階段會選取具有較大之最不顯著位數 (亦即，使用 MidpointRounding.AwayFromZero) 的結果。On the .NET Framework and .NET Core up to .NET Core 2.0, the runtime selects the result with the greater least significant digit (that is, using MidpointRounding.AwayFromZero).
• 在 .NET Core 2.1 與更新版本上，執行階段會選取具有偶數最不顯著位數 (亦即，使用 MidpointRounding.ToEven) 的結果。On .NET Core 2.1 and later, the runtime selects the result with an even least significant digit (that is, using MidpointRounding.ToEven).

注意

有效位數規範可判斷結果字串中的位數。The precision specifier determines the number of digits in the result string. 若要使用前置或尾端空格填補結果字串，請使用複合格式設定功能，並在格式項目中定義「對齊元件」。To pad a result string with leading or trailing spaces, use the composite formatting feature and define an alignment component in the format item.

"C" 或 "c""C" or "c" 貨幣Currency 結果：貨幣值。Result: A currency value.

123.456 （"C"，en-us）-> \$123.46123.456 ("C", en-US) -> \$123.46

123.456 ("C", fr-FR) -> 123,46 €123.456 ("C", fr-FR) -> 123,46 €

123.456 ("C", ja-JP) -> ¥123123.456 ("C", ja-JP) -> ¥123

-123.456 （"C3"，en-us）-> （\$123.456）-123.456 ("C3", en-US) -> (\$123.456)

-123.456 ("C3", fr-FR) -> -123,456 €-123.456 ("C3", fr-FR) -> -123,456 €

-123.456 ("C3", ja-JP) -> -¥123.456-123.456 ("C3", ja-JP) -> -¥123.456
"D" 或 "d""D" or "d" DecimalDecimal 結果：帶選擇性負號的整數位數。Result: Integer digits with optional negative sign.

1234 ("D") -> 12341234 ("D") -> 1234

-1234 ("D6") -> -001234-1234 ("D6") -> -001234
"E" 或 "e""E" or "e" 指數 (科學記號)Exponential (scientific) 結果：指數標記法。Result: Exponential notation.

1052.0329112756 ("E", en-US) -> 1.052033E+0031052.0329112756 ("E", en-US) -> 1.052033E+003

1052.0329112756 ("e", fr-FR) -> 1,052033e+0031052.0329112756 ("e", fr-FR) -> 1,052033e+003

-1052.0329112756 ("e2", en-US) -> -1.05e+003-1052.0329112756 ("e2", en-US) -> -1.05e+003

-1052.0329112756 ("E2", fr-FR) -> -1,05E+003-1052.0329112756 ("E2", fr-FR) -> -1,05E+003
"F" 或 "f""F" or "f" 固定點Fixed-point 結果：帶選擇性負號的整數和小數位數。Result: Integral and decimal digits with optional negative sign.

1234.567 ("F", en-US) -> 1234.571234.567 ("F", en-US) -> 1234.57

1234.567 ("F", de-DE) -> 1234,571234.567 ("F", de-DE) -> 1234,57

1234 ("F1", en-US) -> 1234.01234 ("F1", en-US) -> 1234.0

1234 ("F1", de-DE) -> 1234,01234 ("F1", de-DE) -> 1234,0

-1234.56 ("F4", en-US) -> -1234.5600-1234.56 ("F4", en-US) -> -1234.5600

-1234.56 ("F4", de-DE) -> -1234,5600-1234.56 ("F4", de-DE) -> -1234,5600
"G" 或 "g""G" or "g" 一般General 結果：固定點和科學記號標記法兩者中最精簡的一個。Result: The more compact of either fixed-point or scientific notation.

-123.456 ("G", en-US) -> -123.456-123.456 ("G", en-US) -> -123.456

-123.456 ("G", sv-SE) -> -123,456-123.456 ("G", sv-SE) -> -123,456

123.4546 ("G4", en-US) -> 123.5123.4546 ("G4", en-US) -> 123.5

123.4546 ("G4", sv-SE) -> 123,5123.4546 ("G4", sv-SE) -> 123,5

-1.234567890e-25 ("G", en-US) -> -1.23456789E-25-1.234567890e-25 ("G", en-US) -> -1.23456789E-25

-1.234567890e-25 ("G", sv-SE) -> -1,23456789E-25-1.234567890e-25 ("G", sv-SE) -> -1,23456789E-25
"N" 或 "n""N" or "n" numberNumber 結果：帶選擇性負號的整數和小數位數、群組分隔符號，以及小數分隔符號。Result: Integral and decimal digits, group separators, and a decimal separator with optional negative sign.

1234.567 ("N", en-US) -> 1,234.571234.567 ("N", en-US) -> 1,234.57

1234.567 ("N", ru-RU) -> 1 234,571234.567 ("N", ru-RU) -> 1 234,57

1234 ("N1", en-US) -> 1,234.01234 ("N1", en-US) -> 1,234.0

1234 ("N1", ru-RU) -> 1 234,01234 ("N1", ru-RU) -> 1 234,0

-1234.56 ("N3", en-US) -> -1,234.560-1234.56 ("N3", en-US) -> -1,234.560

-1234.56 ("N3", ru-RU) -> -1 234,560-1234.56 ("N3", ru-RU) -> -1 234,560
"P" 或 "p""P" or "p" 百分比Percent 結果：乘以 100 並加上百分比符號來顯示的數字。Result: Number multiplied by 100 and displayed with a percent symbol.

1 ("P", en-US) -> 100.00 %1 ("P", en-US) -> 100.00 %

1 ("P", fr-FR) -> 100,00 %1 ("P", fr-FR) -> 100,00 %

-0.39678 ("P1", en-US) -> -39.7 %-0.39678 ("P1", en-US) -> -39.7 %

-0.39678 ("P1", fr-FR) -> -39,7 %-0.39678 ("P1", fr-FR) -> -39,7 %
"R" 或 "r""R" or "r" 來回Round-trip 結果：可以來回轉換為相同數字的字串。Result: A string that can round-trip to an identical number.

123456789.12345678 ("R") -> 123456789.12345678123456789.12345678 ("R") -> 123456789.12345678

-1234567890.12345678 ("R") -> -1234567890.1234567-1234567890.12345678 ("R") -> -1234567890.1234567
"X" 或 "x""X" or "x" 十六進位Hexadecimal 結果：十六進位字串。Result: A hexadecimal string.

255 ("X") -> FF255 ("X") -> FF

-1 ("x") -> ff-1 ("x") -> ff

255 ("x4") -> 00ff255 ("x4") -> 00ff

-1 ("X4") -> 00FF-1 ("X4") -> 00FF

## 使用標準數值格式字串Using Standard Numeric Format Strings

• 您可以將它傳遞至具有 ToString 參數之 format 方法的多載。It can be passed to an overload of the ToString method that has a format parameter. 下列範例會以目前的文化特定 (在此範例中是 en-US 文化特性) 來將數值格式化為貨幣字串。The following example formats a numeric value as a currency string in the current culture (in this case, the en-US culture).

Decimal value = static_cast<Decimal>(123.456);
Console::WriteLine(value.ToString("C2"));
// Displays $123.46  decimal value = 123.456m; Console.WriteLine(value.ToString("C2")); // Displays$123.46

Dim value As Decimal = 123.456d
Console.WriteLine(value.ToString("C2"))
' Displays $123.46  • 您可以在搭配 String.FormatConsole.WriteLineStringBuilder.AppendFormat 等方法一起使用的格式項目中，提供它作為 formatString 引數。It can be supplied as the formatString argument in a format item used with such methods as String.Format, Console.WriteLine, and StringBuilder.AppendFormat. 如需詳細資訊，請參閱複合格式設定For more information, see Composite Formatting. 下列範例會使用格式項目，在字串中插入貨幣值。The following example uses a format item to insert a currency value in a string. Decimal value = static_cast<Decimal>(123.456); Console::WriteLine("Your account balance is {0:C2}.", value); // Displays "Your account balance is$123.46."

decimal value = 123.456m;
Console.WriteLine("Your account balance is {0:C2}.", value);
// Displays "Your account balance is $123.46."  Dim value As Decimal = 123.456d Console.WriteLine("Your account balance is {0:C2}.", value) ' Displays "Your account balance is$123.46."


您可以選擇性地提供 alignment 引數，來指定數值欄位的寬度，以及其值為靠右或靠左對齊。Optionally, you can supply an alignment argument to specify the width of the numeric field and whether its value is right- or left-aligned. 下列範例在 28 個字元的欄位中將貨幣值靠左對齊，並在 14 個字元的欄位中將貨幣值靠右對齊。The following example left-aligns a currency value in a 28-character field, and it right-aligns a currency value in a 14-character field.

array<Decimal>^ amounts = { static_cast<Decimal>(16305.32),
static_cast<Decimal>(18794.16) };
Console::WriteLine("   Beginning Balance           Ending Balance");
Console::WriteLine("   {0,-28:C2}{1,14:C2}", amounts[0], amounts[1]);
// Displays:
//        Beginning Balance           Ending Balance
//        $16,305.32$18,794.16

decimal[] amounts = { 16305.32m, 18794.16m };
Console.WriteLine("   Beginning Balance           Ending Balance");
Console.WriteLine("   {0,-28:C2}{1,14:C2}", amounts[0], amounts[1]);
// Displays:
//        Beginning Balance           Ending Balance
//        $16,305.32$18,794.16

Dim amounts() As Decimal = { 16305.32d, 18794.16d }
Console.WriteLine("   Beginning Balance           Ending Balance")
Console.WriteLine("   {0,-28:C2}{1,14:C2}", amounts(0), amounts(1))
' Displays:
'        Beginning Balance           Ending Balance
'        $16,305.32$18,794.16

• 它可以提供作為字串內插補點的插補運算式項目中的 formatString 引數。It can be supplied as the formatString argument in an interpolated expression item of an interpolated string. 如需詳細資訊，請參閱 C# 參考中的字串內插補點主題，或 Visual Basic 參考中的字串內插補點主題。For more information, see the String interpolation topic in the C# reference or the Interpolated strings topic in the Visual Basic reference.

## 貨幣 ("C") 格式規範The Currency ("C") Format Specifier

"C" (表示貨幣) 格式規範會將數字轉換為表示貨幣金額的字串。The "C" (or currency) format specifier converts a number to a string that represents a currency amount. 精確度規範表示在結果字串中所需要的小數位數。The precision specifier indicates the desired number of decimal places in the result string. 如果省略精確度規範，則預設有效位數會由 NumberFormatInfo.CurrencyDecimalDigits 屬性定義。If the precision specifier is omitted, the default precision is defined by the NumberFormatInfo.CurrencyDecimalDigits property.

NumberFormatInfo 屬性NumberFormatInfo property 描述Description
CurrencyPositivePattern 定義正值的貨幣符號位置。Defines the placement of the currency symbol for positive values.
CurrencyNegativePattern 定義負值的貨幣符號位置，並指定負號是以括號還是 NegativeSign 屬性來表示。Defines the placement of the currency symbol for negative values, and specifies whether the negative sign is represented by parentheses or the NegativeSign property.
NegativeSign 定義在 CurrencyNegativePattern 表示不使用括號時所使用的負號。Defines the negative sign used if CurrencyNegativePattern indicates that parentheses are not used.
CurrencySymbol 定義貨幣符號。Defines the currency symbol.
CurrencyDecimalDigits 定義貨幣值中的預設小數位數。Defines the default number of decimal digits in a currency value. 您可以使用有效位數規範來覆寫這個值。This value can be overridden by using the precision specifier.
CurrencyDecimalSeparator 定義分隔整數與小數位數的字串。Defines the string that separates integral and decimal digits.
CurrencyGroupSeparator 定義分隔整數群組的字串。Defines the string that separates groups of integral numbers.
CurrencyGroupSizes 定義整數部分的每個群組中出現的整數位數。Defines the number of integer digits that appear in a group.

double value = 12345.6789;
Console::WriteLine(value.ToString("C", CultureInfo::CurrentCulture));

Console::WriteLine(value.ToString("C3", CultureInfo::CurrentCulture));

Console::WriteLine(value.ToString("C3",
CultureInfo::CreateSpecificCulture("da-DK")));
// The example displays the following output on a system whose
// current culture is English (United States):
//       $12,345.68 //$12,345.679
//       kr 12.345,679

double value = 12345.6789;
Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));

Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));

Console.WriteLine(value.ToString("C3",
CultureInfo.CreateSpecificCulture("da-DK")));
// The example displays the following output on a system whose
// current culture is English (United States):
//       $12,345.68 //$12,345.679
//       12.345,679 kr

Dim value As Double = 12345.6789
Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture))

Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture))

Console.WriteLine(value.ToString("C3", _
CultureInfo.CreateSpecificCulture("da-DK")))
' The example displays the following output on a system whose
' current culture is English (United States):
'       $12,345.68 '$12,345.679
'       kr 12.345,679


## 十進位 ("D") 格式規範The Decimal ("D") Format Specifier

"D" (表示十進位) 格式規範會將數字轉換為十進位數 (0-9) 的字串，如果數字為負數，則在前面加上負號。The "D" (or decimal) format specifier converts a number to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative. 只有整數類資料類型 (Integral Type) 才支援這個格式。This format is supported only for integral types.

NumberFormatInfo 屬性NumberFormatInfo property 描述Description
NegativeSign 定義表示數字為負數的字串。Defines the string that indicates that a number is negative.

int value;

value = 12345;
Console::WriteLine(value.ToString("D"));
// Displays 12345
Console::WriteLine(value.ToString("D8"));
// Displays 00012345

value = -12345;
Console::WriteLine(value.ToString("D"));
// Displays -12345
Console::WriteLine(value.ToString("D8"));
// Displays -00012345

int value;

value = 12345;
Console.WriteLine(value.ToString("D"));
// Displays 12345
Console.WriteLine(value.ToString("D8"));
// Displays 00012345

value = -12345;
Console.WriteLine(value.ToString("D"));
// Displays -12345
Console.WriteLine(value.ToString("D8"));
// Displays -00012345

Dim value As Integer

value = 12345
Console.WriteLine(value.ToString("D"))
' Displays 12345
Console.WriteLine(value.ToString("D8"))
' Displays 00012345

value = -12345
Console.WriteLine(value.ToString("D"))
' Displays -12345
Console.WriteLine(value.ToString("D8"))
' Displays -00012345


## 指數 ("E") 格式規範The Exponential ("E") Format Specifier

NumberFormatInfo 屬性NumberFormatInfo property 描述Description
NegativeSign 定義表示數字在係數和指數部分都是負數的字串。Defines the string that indicates that a number is negative for both the coefficient and exponent.
NumberDecimalSeparator 定義將係數中的整數位數與小數位數分隔的字串。Defines the string that separates the integral digit from decimal digits in the coefficient.
PositiveSign 定義表示指數為正數的字串。Defines the string that indicates that an exponent is positive.

double value = 12345.6789;
Console::WriteLine(value.ToString("E", CultureInfo::InvariantCulture));
// Displays 1.234568E+004

Console::WriteLine(value.ToString("E10", CultureInfo::InvariantCulture));
// Displays 1.2345678900E+004

Console::WriteLine(value.ToString("e4", CultureInfo::InvariantCulture));
// Displays 1.2346e+004

Console::WriteLine(value.ToString("E",
CultureInfo::CreateSpecificCulture("fr-FR")));
// Displays 1,234568E+004

double value = 12345.6789;
Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture));
// Displays 1.234568E+004

Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));
// Displays 1.2345678900E+004

Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture));
// Displays 1.2346e+004

Console.WriteLine(value.ToString("E",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays 1,234568E+004

Dim value As Double = 12345.6789
Console.WriteLine(value.ToString("E", CultureInfo.InvariantCulture))
' Displays 1.234568E+004

Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture))
' Displays 1.2345678900E+004

Console.WriteLine(value.ToString("e4", CultureInfo.InvariantCulture))
' Displays 1.2346e+004

Console.WriteLine(value.ToString("E", _
CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays 1,234568E+004


## 固定點 ("F") 格式規範The Fixed-Point ("F") Format Specifier

NumberFormatInfo 屬性NumberFormatInfo property 描述Description
NegativeSign 定義表示數字為負數的字串。Defines the string that indicates that a number is negative.
NumberDecimalSeparator 定義分隔整數位數與小數位數的字串。Defines the string that separates integral digits from decimal digits.
NumberDecimalDigits 定義小數位數的預設數目。Defines the default number of decimal digits. 您可以使用有效位數規範來覆寫這個值。This value can be overridden by using the precision specifier.

int integerNumber;
integerNumber = 17843;
Console::WriteLine(integerNumber.ToString("F",
CultureInfo::InvariantCulture));
// Displays 17843.00

integerNumber = -29541;
Console::WriteLine(integerNumber.ToString("F3",
CultureInfo::InvariantCulture));
// Displays -29541.000

double doubleNumber;
doubleNumber = 18934.1879;
Console::WriteLine(doubleNumber.ToString("F", CultureInfo::InvariantCulture));
// Displays 18934.19

Console::WriteLine(doubleNumber.ToString("F0", CultureInfo::InvariantCulture));
// Displays 18934

doubleNumber = -1898300.1987;
Console::WriteLine(doubleNumber.ToString("F1", CultureInfo::InvariantCulture));
// Displays -1898300.2

Console::WriteLine(doubleNumber.ToString("F3",
CultureInfo::CreateSpecificCulture("es-ES")));
// Displays -1898300,199

int integerNumber;
integerNumber = 17843;
Console.WriteLine(integerNumber.ToString("F",
CultureInfo.InvariantCulture));
// Displays 17843.00

integerNumber = -29541;
Console.WriteLine(integerNumber.ToString("F3",
CultureInfo.InvariantCulture));
// Displays -29541.000

double doubleNumber;
doubleNumber = 18934.1879;
Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture));
// Displays 18934.19

Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture));
// Displays 18934

doubleNumber = -1898300.1987;
Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture));
// Displays -1898300.2

Console.WriteLine(doubleNumber.ToString("F3",
CultureInfo.CreateSpecificCulture("es-ES")));
// Displays -1898300,199

Dim integerNumber As Integer
integerNumber = 17843
Console.WriteLine(integerNumber.ToString("F", CultureInfo.InvariantCulture))
' Displays 17843.00

integerNumber = -29541
Console.WriteLine(integerNumber.ToString("F3", CultureInfo.InvariantCulture))
' Displays -29541.000

Dim doubleNumber As Double
doubleNumber = 18934.1879
Console.WriteLine(doubleNumber.ToString("F", CultureInfo.InvariantCulture))
' Displays 18934.19

Console.WriteLine(doubleNumber.ToString("F0", CultureInfo.InvariantCulture))
' Displays 18934

doubleNumber = -1898300.1987
Console.WriteLine(doubleNumber.ToString("F1", CultureInfo.InvariantCulture))
' Displays -1898300.2

Console.WriteLine(doubleNumber.ToString("F3", _
CultureInfo.CreateSpecificCulture("es-ES")))
' Displays -1898300,199


## 一般 ("G") 格式規範The General ("G") Format Specifier

3 位數3 digits
5 位數5 digits
10 位數10 digits
Int64 19 位數19 digits
UInt64 20 位數20 digits
BigInteger 無限制 (如同 "R")Unlimited (same as "R")
Single 7 位數7 digits
Double 15 位數15 digits
Decimal 29 位數29 digits

double original = 0.84551240822557006;
var rSpecifier = original.ToString("R");
var g17Specifier = original.ToString("G17");

var rValue = Double.Parse(rSpecifier);
var g17Value = Double.Parse(g17Specifier);

Console.WriteLine($"{original:G17} = {rSpecifier} (R): {original.Equals(rValue)}"); Console.WriteLine($"{original:G17} = {g17Specifier} (G17): {original.Equals(g17Value)}");
// The example displays the following output:
//     0.84551240822557006 = 0.84551240822557: False
//     0.84551240822557006 = 0.84551240822557006: True

Module Example
Public Sub Main()
Dim original As Double = 0.84551240822557006
Dim rSpecifier = original.ToString("R")
Dim g17Specifier = original.ToString("G17")

Dim rValue = Double.Parse(rSpecifier)
Dim g17Value = Double.Parse(g17Specifier)

Console.WriteLine($"{original:G17} = {rSpecifier} (R): {original.Equals(rValue)}") Console.WriteLine($"{original:G17} = {g17Specifier} (G17): {original.Equals(g17Value)}")
End Sub
End Module
' The example displays the following output:
'     0.84551240822557006 = 0.84551240822557 (R): False
'     0.84551240822557006 = 0.84551240822557006 (G17): True



NumberFormatInfo 屬性NumberFormatInfo property 描述Description
NegativeSign 定義表示數字為負數的字串。Defines the string that indicates that a number is negative.
NumberDecimalSeparator 定義分隔整數位數與小數位數的字串。Defines the string that separates integral digits from decimal digits.
PositiveSign 定義表示指數為正數的字串。Defines the string that indicates that an exponent is positive.

double number;

number = 12345.6789;
Console::WriteLine(number.ToString("G", CultureInfo::InvariantCulture));
// Displays  12345.6789
Console::WriteLine(number.ToString("G",
CultureInfo::CreateSpecificCulture("fr-FR")));
// Displays 12345,6789

Console::WriteLine(number.ToString("G7", CultureInfo::InvariantCulture));
// Displays 12345.68

number = .0000023;
Console::WriteLine(number.ToString("G", CultureInfo::InvariantCulture));
// Displays 2.3E-06
Console::WriteLine(number.ToString("G",
CultureInfo::CreateSpecificCulture("fr-FR")));
// Displays 2,3E-06

number = .0023;
Console::WriteLine(number.ToString("G", CultureInfo::InvariantCulture));
// Displays 0.0023

number = 1234;
Console::WriteLine(number.ToString("G2", CultureInfo::InvariantCulture));
// Displays 1.2E+03

number = Math::PI;
Console::WriteLine(number.ToString("G5", CultureInfo::InvariantCulture));
// Displays 3.1416

double number;

number = 12345.6789;
Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
// Displays  12345.6789
Console.WriteLine(number.ToString("G",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays 12345,6789

Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture));
// Displays 12345.68

number = .0000023;
Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
// Displays 2.3E-06
Console.WriteLine(number.ToString("G",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays 2,3E-06

number = .0023;
Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture));
// Displays 0.0023

number = 1234;
Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture));
// Displays 1.2E+03

number = Math.PI;
Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture));
// Displays 3.1416

Dim number As Double

number = 12345.6789
Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
' Displays  12345.6789
Console.WriteLine(number.ToString("G", _
CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays 12345,6789

Console.WriteLine(number.ToString("G7", CultureInfo.InvariantCulture))
' Displays 12345.68

number = .0000023
Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
' Displays 2.3E-06
Console.WriteLine(number.ToString("G", _
CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays 2,3E-06

number = .0023
Console.WriteLine(number.ToString("G", CultureInfo.InvariantCulture))
' Displays 0.0023

number = 1234
Console.WriteLine(number.ToString("G2", CultureInfo.InvariantCulture))
' Displays 1.2E+03

number = Math.Pi
Console.WriteLine(number.ToString("G5", CultureInfo.InvariantCulture))
' Displays 3.1416


## 數值 ("N") 格式規範The Numeric ("N") Format Specifier

NumberFormatInfo 屬性NumberFormatInfo property 描述Description
NegativeSign 定義表示數字為負數的字串。Defines the string that indicates that a number is negative.
NumberNegativePattern 定義負值的格式，並指定負號是以括號還是 NegativeSign 屬性來表示。Defines the format of negative values, and specifies whether the negative sign is represented by parentheses or the NegativeSign property.
NumberGroupSizes 定義在群組分隔符號之間出現的整數位數。Defines the number of integral digits that appear between group separators.
NumberGroupSeparator 定義分隔整數群組的字串。Defines the string that separates groups of integral numbers.
NumberDecimalSeparator 定義分隔整數與小數位數的字串。Defines the string that separates integral and decimal digits.
NumberDecimalDigits 定義小數位數的預設數目。Defines the default number of decimal digits. 您可以使用精確度規範來覆寫這個值。This value can be overridden by using a precision specifier.

double dblValue = -12445.6789;
Console::WriteLine(dblValue.ToString("N", CultureInfo::InvariantCulture));
// Displays -12,445.68
Console::WriteLine(dblValue.ToString("N1",
CultureInfo::CreateSpecificCulture("sv-SE")));
// Displays -12 445,7

int intValue = 123456789;
Console::WriteLine(intValue.ToString("N1", CultureInfo::InvariantCulture));
// Displays 123,456,789.0

double dblValue = -12445.6789;
Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture));
// Displays -12,445.68
Console.WriteLine(dblValue.ToString("N1",
CultureInfo.CreateSpecificCulture("sv-SE")));
// Displays -12 445,7

int intValue = 123456789;
Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture));
// Displays 123,456,789.0

Dim dblValue As Double = -12445.6789
Console.WriteLine(dblValue.ToString("N", CultureInfo.InvariantCulture))
' Displays -12,445.68
Console.WriteLine(dblValue.ToString("N1", _
CultureInfo.CreateSpecificCulture("sv-SE")))
' Displays -12 445,7

Dim intValue As Integer = 123456789
Console.WriteLine(intValue.ToString("N1", CultureInfo.InvariantCulture))
' Displays 123,456,789.0


## 百分比 ("P") 格式規範The Percent ("P") Format Specifier

NumberFormatInfo 屬性NumberFormatInfo property 描述Description
PercentPositivePattern 定義正值的百分比符號位置。Defines the placement of the percent symbol for positive values.
PercentNegativePattern 定義負值的百分比符號位置和負號位置。Defines the placement of the percent symbol and the negative symbol for negative values.
NegativeSign 定義表示數字為負數的字串。Defines the string that indicates that a number is negative.
PercentSymbol 定義百分比符號。Defines the percent symbol.
PercentDecimalDigits 定義百分比值中的預設小數位數。Defines the default number of decimal digits in a percentage value. 您可以使用有效位數規範來覆寫這個值。This value can be overridden by using the precision specifier.
PercentDecimalSeparator 定義分隔整數與小數位數的字串。Defines the string that separates integral and decimal digits.
PercentGroupSeparator 定義分隔整數群組的字串。Defines the string that separates groups of integral numbers.
PercentGroupSizes 定義整數部分的每個群組中出現的整數位數。Defines the number of integer digits that appear in a group.

double number = .2468013;
Console::WriteLine(number.ToString("P", CultureInfo::InvariantCulture));
// Displays 24.68 %
Console::WriteLine(number.ToString("P",
CultureInfo::CreateSpecificCulture("hr-HR")));
// Displays 24,68%
Console::WriteLine(number.ToString("P1", CultureInfo::InvariantCulture));
// Displays 24.7 %

double number = .2468013;
Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
// Displays 24.68 %
Console.WriteLine(number.ToString("P",
CultureInfo.CreateSpecificCulture("hr-HR")));
// Displays 24,68%
Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture));
// Displays 24.7 %

Dim number As Double = .2468013
Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture))
' Displays 24.68 %
Console.WriteLine(number.ToString("P", _
CultureInfo.CreateSpecificCulture("hr-HR")))
' Displays 24,68%
Console.WriteLine(number.ToString("P1", CultureInfo.InvariantCulture))
' Displays 24.7 %


## 來回 ("R") 格式規範The Round-trip ("R") Format Specifier

NumberFormatInfo 屬性NumberFormatInfo property 描述Description
NegativeSign 定義表示數字為負數的字串。Defines the string that indicates that a number is negative.
NumberDecimalSeparator 定義分隔整數位數與小數位數的字串。Defines the string that separates integral digits from decimal digits.
PositiveSign 定義表示指數為正數的字串。Defines the string that indicates that an exponent is positive.

#using <System.Numerics.dll>

using namespace System;
using namespace System::Numerics;

void main()
{
BigInteger value = BigInteger::Pow(Int64::MaxValue, 2);
Console::WriteLine(value.ToString("R"));
}
// The example displays the following output:
//      85070591730234615847396907784232501249


using System;
using System.Numerics;

public class Example
{
public static void Main()
{
var value = BigInteger.Pow(Int64.MaxValue, 2);
Console.WriteLine(value.ToString("R"));
}
}
// The example displays the following output:
//      85070591730234615847396907784232501249

Imports System.Numerics

Module Example
Public Sub Main()
Dim value = BigInteger.Pow(Int64.MaxValue, 2)
Console.WriteLine(value.ToString("R"))
End Sub
End Module
' The example displays the following output:
'      85070591730234615847396907784232501249


Console.WriteLine("Attempting to round-trip a Double with 'R':");
double initialValue = 0.6822871999174;
string valueString = initialValue.ToString("R",
CultureInfo.InvariantCulture);
double roundTripped = double.Parse(valueString,
CultureInfo.InvariantCulture);
Console.WriteLine("{0:R} = {1:R}: {2}\n",
initialValue, roundTripped, initialValue.Equals(roundTripped));

Console.WriteLine("Attempting to round-trip a Double with 'G17':");
string valueString17 = initialValue.ToString("G17",
CultureInfo.InvariantCulture);
double roundTripped17 = double.Parse(valueString17,
CultureInfo.InvariantCulture);
Console.WriteLine("{0:R} = {1:R}: {2}\n",
initialValue, roundTripped17, initialValue.Equals(roundTripped17));
// If compiled to an application that targets anycpu or x64 and run on an x64 system,
// the example displays the following output:
//       Attempting to round-trip a Double with 'R':
//       0.6822871999174 = 0.68228719991740006: False
//
//       Attempting to round-trip a Double with 'G17':
//       0.6822871999174 = 0.6822871999174: True

Imports System.Globalization

Module Example
Public Sub Main()
Console.WriteLine("Attempting to round-trip a Double with 'R':")
Dim initialValue As Double = 0.6822871999174
Dim valueString As String = initialValue.ToString("R",
CultureInfo.InvariantCulture)
Dim roundTripped As Double = Double.Parse(valueString,
CultureInfo.InvariantCulture)
Console.WriteLine("{0:R} = {1:R}: {2}",
initialValue, roundTripped, initialValue.Equals(roundTripped))
Console.WriteLine()

Console.WriteLine("Attempting to round-trip a Double with 'G17':")
Dim valueString17 As String = initialValue.ToString("G17",
CultureInfo.InvariantCulture)
Dim roundTripped17 As Double = double.Parse(valueString17,
CultureInfo.InvariantCulture)
Console.WriteLine("{0:R} = {1:R}: {2}",
initialValue, roundTripped17, initialValue.Equals(roundTripped17))
End Sub
End Module
' If compiled to an application that targets anycpu or x64 and run on an x64 system,
' the example displays the following output:
'       Attempting to round-trip a Double with 'R':
'       0.6822871999174 = 0.68228719991740006: False
'
'       Attempting to round-trip a Double with 'G17':
'       0.6822871999174 = 0.6822871999174: True


## 十六進位 ("X") 格式規範The Hexadecimal ("X") Format Specifier

int value;

value = 0x2045e;
Console::WriteLine(value.ToString("x"));
// Displays 2045e
Console::WriteLine(value.ToString("X"));
// Displays 2045E
Console::WriteLine(value.ToString("X8"));
// Displays 0002045E

value = 123456789;
Console::WriteLine(value.ToString("X"));
// Displays 75BCD15
Console::WriteLine(value.ToString("X2"));
// Displays 75BCD15

int value;

value = 0x2045e;
Console.WriteLine(value.ToString("x"));
// Displays 2045e
Console.WriteLine(value.ToString("X"));
// Displays 2045E
Console.WriteLine(value.ToString("X8"));
// Displays 0002045E

value = 123456789;
Console.WriteLine(value.ToString("X"));
// Displays 75BCD15
Console.WriteLine(value.ToString("X2"));
// Displays 75BCD15

Dim value As Integer

value = &h2045e
Console.WriteLine(value.ToString("x"))
' Displays 2045e
Console.WriteLine(value.ToString("X"))
' Displays 2045E
Console.WriteLine(value.ToString("X8"))
' Displays 0002045E

value = 123456789
Console.WriteLine(value.ToString("X"))
' Displays 75BCD15
Console.WriteLine(value.ToString("X2"))
' Displays 75BCD15


## 注意Notes

### 控制台設定值Control Panel Settings

[控制台] 中 [ 地區及語言選項] 項目的設定會影響格式化作業所產生的結果字串。The settings in the Regional and Language Options item in Control Panel influence the result string produced by a formatting operation. 這些設定是用來初始化與目前執行緒文化特性相關的 NumberFormatInfo 物件，該物件會提供用來管理格式的值。Those settings are used to initialize the NumberFormatInfo object associated with the current thread culture, which provides values used to govern formatting. 使用不同設定的電腦會產生不同的結果字串。Computers that use different settings generate different result strings.

## 範例Example

// Display string representations of numbers for en-us culture
CultureInfo ci = new CultureInfo("en-us");

// Output floating point values
double floating = 10761.937554;
Console.WriteLine("C: {0}",
floating.ToString("C", ci));           // Displays "C: $10,761.94" Console.WriteLine("E: {0}", floating.ToString("E03", ci)); // Displays "E: 1.076E+004" Console.WriteLine("F: {0}", floating.ToString("F04", ci)); // Displays "F: 10761.9376" Console.WriteLine("G: {0}", floating.ToString("G", ci)); // Displays "G: 10761.937554" Console.WriteLine("N: {0}", floating.ToString("N03", ci)); // Displays "N: 10,761.938" Console.WriteLine("P: {0}", (floating/10000).ToString("P02", ci)); // Displays "P: 107.62 %" Console.WriteLine("R: {0}", floating.ToString("R", ci)); // Displays "R: 10761.937554" Console.WriteLine(); // Output integral values int integral = 8395; Console.WriteLine("C: {0}", integral.ToString("C", ci)); // Displays "C:$8,395.00"
Console.WriteLine("D: {0}",
integral.ToString("D6", ci));          // Displays "D: 008395"
Console.WriteLine("E: {0}",
integral.ToString("E03", ci));         // Displays "E: 8.395E+003"
Console.WriteLine("F: {0}",
integral.ToString("F01", ci));         // Displays "F: 8395.0"
Console.WriteLine("G: {0}",
integral.ToString("G", ci));           // Displays "G: 8395"
Console.WriteLine("N: {0}",
integral.ToString("N01", ci));         // Displays "N: 8,395.0"
Console.WriteLine("P: {0}",
(integral/10000.0).ToString("P02", ci)); // Displays "P: 83.95 %"
Console.WriteLine("X: 0x{0}",
integral.ToString("X", ci));           // Displays "X: 0x20CB"
Console.WriteLine();

Option Strict On

Imports System.Globalization

Module NumericFormats
Public Sub Main()
' Display string representations of numbers for en-us culture
Dim ci As New CultureInfo("en-us")

' Output floating point values
Dim floating As Double = 10761.937554
Console.WriteLine("C: {0}", _
floating.ToString("C", ci))           ' Displays "C: $10,761.94" Console.WriteLine("E: {0}", _ floating.ToString("E03", ci)) ' Displays "E: 1.076E+004" Console.WriteLine("F: {0}", _ floating.ToString("F04", ci)) ' Displays "F: 10761.9376" Console.WriteLine("G: {0}", _ floating.ToString("G", ci)) ' Displays "G: 10761.937554" Console.WriteLine("N: {0}", _ floating.ToString("N03", ci)) ' Displays "N: 10,761.938" Console.WriteLine("P: {0}", _ (floating/10000).ToString("P02", ci)) ' Displays "P: 107.62 %" Console.WriteLine("R: {0}", _ floating.ToString("R", ci)) ' Displays "R: 10761.937554" Console.WriteLine() ' Output integral values Dim integral As Integer = 8395 Console.WriteLine("C: {0}", _ integral.ToString("C", ci)) ' Displays "C:$8,395.00"
Console.WriteLine("D: {0}", _
integral.ToString("D6"))              ' Displays "D: 008395"
Console.WriteLine("E: {0}", _
integral.ToString("E03", ci))         ' Displays "E: 8.395E+003"
Console.WriteLine("F: {0}", _
integral.ToString("F01", ci))         ' Displays "F: 8395.0"
Console.WriteLine("G: {0}", _
integral.ToString("G", ci))           ' Displays "G: 8395"
Console.WriteLine("N: {0}", _
integral.ToString("N01", ci))         ' Displays "N: 8,395.0"
Console.WriteLine("P: {0}", _
(integral/10000).ToString("P02", ci)) ' Displays "P: 83.95 %"
Console.WriteLine("X: 0x{0}", _
integral.ToString("X", ci))           ' Displays "X: 0x20CB"
Console.WriteLine()
End Sub
End Module