標準數值格式字串
標準數值格式字串會用來格式化一般數字型別。標準數值格式字串會採用 Axx 格式,其中 A 是一個字母字元,稱為格式規範,而 xx 則是一個選擇性的整數,稱為精確度規範。精確度規範的範圍從 0 到 99,而且會影響結果內的位數。任何包含一個以上字母字元 (包含泛空白字元 (White Space)) 的數值格式字串都會解譯為自訂數值格式字串。
下表說明標準數值格式規範。如需每種格式規範所產生之輸出的範例,請參閱標準數值格式字串輸出範例。如需詳細資訊,請參閱表格之後的注意事項。
格式規範 | 名稱 | 說明 |
---|---|---|
C 或 c |
貨幣 |
數值將轉換為表示貨幣數量的字串。轉換是由目前 NumberFormatInfo 物件的貨幣格式資訊所控制。 精確度規範指示所需要的小數位數。如果省略精確度規範,則會使用目前 NumberFormatInfo 物件所提供的預設貨幣精確度。 |
D 或 d |
十進位 |
只有整數類資料型別 (Integral Type) 才支援這個格式。數值將會被轉換為十進位數 (0-9) 的字串,如果數值為負數,則在前面加上負號。 精確度規範指示產生的字串中所需要的最少位數。如果有必要,數值以零填補其左邊,產生精確度規範所指定的位數。 |
E 或 e |
科學 (指數的) |
數字會轉換為 "-d.ddd…E+ddd" 或 "-d.ddd…e+ddd" 型式的字串,其中 "d" 表示數字 (0-9)。字串以負號開始,如果數值為負數的話。在小數點前面永遠會有一個位數。 精確度規範指示小數點之後需要的位數。如果省略精確度規範,則使用小數點之後有六位數的預設值。 格式規範的大小寫指示是否在指數之前加上 'E' 或 'e'。指數永遠由正號或負號和最少三位數所組成。必要時,指數將以零填補來符合指定的最少位數。 |
F 或 f |
固定點 |
數字會轉換為 "-ddd.ddd..." 型式的字串,其中 "d" 表示數字 (0-9)。字串以負號開始,如果數值為負數的話。 精確度規範指示所需要的小數位數。如果省略整數位數規範,則使用目前 NumberFormatInfo 物件提供的預設數字整數位數。 |
G 或 g |
一般 |
數字會轉換為固定點或科學標記法中最精簡的一個,端視數字的型別和精確度規範是否存在而定。如果精確度規範已省略或為零,數字的型別將決定預設的精確度,如下列清單所顯示的。
如果以科學標記法來表示數字所產生的指數大於 -5 而且小於精確度規範,則會使用固定點標記法,否則使用科學標記法。必要時,結果會包含小數點並省略字串後端的零。如果精確度規範存在而且結果中的有效位數超過指定的精確度,那麼超出的後端位數會藉由四捨五入來移除。 前面規則的例外是如果數字為 Decimal 而精確度規範已省略的情形。在該情形下,永遠使用固定點標記法並且保留後端的零。 要是使用科學標記法,如果格式規範為 'G',結果中的指數前面會加上 'E';如果格式規範為 'g',則加上 'e'。 |
N 或 n |
數字 |
數字會轉換為 "-d,ddd,ddd.ddd…" 格式的字串,其中 '-' 表示負數符號 (若有必要)、'd' 表示數字 (0-9)、',' 表示數字群組之間的千位分隔符號,而 '.' 則表示小數點符號。實際的負數模式、數字群組大小、千位分隔符號和小數分隔符號是由目前的 NumberFormatInfo 物件所指定。 精確度規範指示所需要的小數位數。如果省略整數位數規範,則使用目前 NumberFormatInfo 物件提供的預設數字整數位數。 |
P 或 p |
百分比 |
如果數值為負數,數值會轉換為表示 NumberFormatInfo.PercentNegativePattern 屬性所定義的百分比字串;如果數值為正數,則轉換為表示 NumberFormatInfo.PercentPositivePattern 屬性所定義的百分比字串。轉換的數值要乘以 100,以便呈現為百分比。 精確度規範指示所需要的小數位數。如果省略整數位數規範,則使用目前 NumberFormatInfo 物件提供的預設數字整數位數。 |
R 或 r |
來回 |
只有 Single 和 Double 型別才支援這個格式。來回規範保證轉換為字串的數值將會被剖析還原成相同數值。當數值使用這個規範來格式化時,它首先使用一般格式 (具有 15 位精確度的 Double 和 7 位精確度的 Single) 來測試。如果該值成功地剖析回到相同數值,即會使用一般格式規範來格式化。然而,如果這個值無法成功地剖析回到相同數值,就會使用 17 位數精確度的 Double 和 9 位數精確度的 Single 來格式化這個值。 雖然精確度規範可以存在,仍舊會被忽略。使用來回規範時,這個規範優先於精確度規範。 |
X 或 x |
十六進位 |
只有整數類資料型別 (Integral Type) 才支援這個格式。數值將轉換為十六進位數字的字串。格式規範的大小寫指示是否要使用大寫或小寫字元於大於 9 的十六進位數字。例如,使用 'X' 產生 "ABCDEF",而 'x' 產生 "abcdef"。 精確度規範指示產生的字串中所需要的最少位數。如果有必要,數值以零填補其左邊,產生精確度規範所指定的位數。 |
任何其他單一字元 |
(未知的規範) |
(未知的規範會擲回執行階段格式例外狀況)。 |
注意
控制台設定值
[控制台] 中 [地區及語言選項] 項目的設定會影響格式化作業所產生的結果字串。這些設定是用來初始化與目前執行緒文化特性相關聯的 NumberFormatInfo 物件,而且目前的執行緒文化特性會提供用來管理格式的值。使用不同設定的電腦將會產生不同的結果字串。
NumberFormatInfo 屬性
格式會受到目前 NumberFormatInfo 物件的影響,而此物件是由目前執行緒文化特性隱含提供或由叫用格式之方法的 IFormatProvider 參數明確提供。為該參數指定 NumberFormatInfo 或 CultureInfo 物件。
整數類資料型別和浮點數值型別
標準數值格式規範的某些描述會參考整數類資料型別或浮點數值型別。整數類資料數字型別 (Numeric Type) 有 Byte、SByte、Int16、Int32、Int64、UInt16、UInt32 和 UInt64。浮點數值型別有 Decimal、Single 和 Double。
無限浮點數和 NaN
請注意,不管格式字串為何,如果 Single 或 Double 浮點型別為正無限大、負無限大或不是數字 (NaN),則格式化後的字串分別會是由目前適用之 NumberFormatInfo 物件所指定的 PositiveInfinitySymbol、NegativeInfinitySymbol 或 NaNSymbol 屬性。
範例
下列程式碼範例會格式化整數和浮點數值,透過的方式是使用執行緒目前的文化特性、指定的文化特性,以及所有標準數值格式規範。這個程式碼範例使用兩個特定的數字型別 (Numeric Type),但是會針對任何數字基底型別產生類似的結果 (Byte、SByte、Int16、Int32、Int64、UInt16、UInt32、UInt64、Decimal、Single 和 Double)。
' This code example demonstrates the ToString(String) and
' ToString(String, IFormatProvider) methods for integral and
' floating-point numbers, in conjunction with the standard
' numeric format specifiers.
' This code example uses the System.Int32 integral type and
' the System.Double floating-point type, but would yield
' similar results for any of the numeric types. The integral
' numeric types are System.Byte, SByte, Int16, Int32, Int64,
' UInt16, UInt32, and UInt64. The floating-point numeric types
' are Decimal, Single, and Double.
Imports System
Imports System.Globalization
Imports System.Threading
Class Sample
Public Shared Sub Main()
' Format a negative integer or floating-point number in various ways.
Dim integralVal As Integer = -12345
Dim floatingVal As Double = -1234.567
Dim msgCurrency As String = "(C) Currency: . . . . . . "
Dim msgDecimal As String = "(D) Decimal:. . . . . . . "
Dim msgScientific As String = "(E) Scientific: . . . . . "
Dim msgFixedPoint As String = "(F) Fixed point:. . . . . "
Dim msgGeneral As String = "(G) General (default):. . "
Dim msgNumber As String = "(N) Number: . . . . . . . "
Dim msgPercent As String = "(P) Percent:. . . . . . . "
Dim msgRoundTrip As String = "(R) Round-trip: . . . . . "
Dim msgHexadecimal As String = "(X) Hexadecimal:. . . . . "
Dim msg1 As String = "Use ToString(String) and the current thread culture." + vbLf
Dim msg2 As String = "Use ToString(String, IFormatProvider) and a specified culture." + vbLf
Dim msgCulture As String = "Culture:"
Dim msgIntegralVal As String = "Integral value:"
Dim msgFloatingVal As String = "Floating-point value:"
Dim ci As CultureInfo
'
Console.Clear()
Console.WriteLine("Standard Numeric Format Specifiers:" & vbLf)
' Display the values.
Console.WriteLine(msg1)
' Display the thread current culture, which is used to format the values.
ci = Thread.CurrentThread.CurrentCulture
Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName)
' Display the integral and floating-point values.
Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal)
Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal)
Console.WriteLine()
' Use the format specifiers that are only for integral types.
Console.WriteLine("Format specifiers only for integral types:")
Console.WriteLine(msgDecimal & integralVal.ToString("D"))
Console.WriteLine(msgHexadecimal & integralVal.ToString("X"))
Console.WriteLine()
' Use the format specifier that is only for the Single and Double
' floating-point types.
Console.WriteLine("Format specifier only for the Single and Double types:")
Console.WriteLine(msgRoundTrip & floatingVal.ToString("R"))
Console.WriteLine()
' Use the format specifiers that are for integral or floating-point types.
Console.WriteLine("Format specifiers for integral or floating-point types:")
Console.WriteLine(msgCurrency & floatingVal.ToString("C"))
Console.WriteLine(msgScientific & floatingVal.ToString("E"))
Console.WriteLine(msgFixedPoint & floatingVal.ToString("F"))
Console.WriteLine(msgGeneral & floatingVal.ToString("G"))
Console.WriteLine(msgNumber & floatingVal.ToString("N"))
Console.WriteLine(msgPercent & floatingVal.ToString("P"))
Console.WriteLine()
' Display the same values using a CultureInfo object. The CultureInfo class
' implements IFormatProvider.
Console.WriteLine(msg2)
' Display the culture used to format the values.
' Create a European culture and change its currency symbol to "euro" because
' this particular code example uses a thread current UI culture that cannot
' display the euro symbol (€).
ci = New CultureInfo("de-DE")
ci.NumberFormat.CurrencySymbol = "euro"
Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName)
' Display the integral and floating-point values.
Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal)
Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal)
Console.WriteLine()
' Use the format specifiers that are only for integral types.
Console.WriteLine("Format specifiers only for integral types:")
Console.WriteLine(msgDecimal & integralVal.ToString("D", ci))
Console.WriteLine(msgHexadecimal & integralVal.ToString("X", ci))
Console.WriteLine()
' Use the format specifier that is only for the Single and Double
' floating-point types.
Console.WriteLine("Format specifier only for the Single and Double types:")
Console.WriteLine(msgRoundTrip & floatingVal.ToString("R", ci))
Console.WriteLine()
' Use the format specifiers that are for integral or floating-point types.
Console.WriteLine("Format specifiers for integral or floating-point types:")
Console.WriteLine(msgCurrency & floatingVal.ToString("C", ci))
Console.WriteLine(msgScientific & floatingVal.ToString("E", ci))
Console.WriteLine(msgFixedPoint & floatingVal.ToString("F", ci))
Console.WriteLine(msgGeneral & floatingVal.ToString("G", ci))
Console.WriteLine(msgNumber & floatingVal.ToString("N", ci))
Console.WriteLine(msgPercent & floatingVal.ToString("P", ci))
Console.WriteLine()
End Sub 'Main
End Class 'Sample
'
'This code example produces the following results:
'
'Standard Numeric Format Specifiers:
'
'Use ToString(String) and the current thread culture.
'
'Culture: English (United States)
'Integral value: -12345
'Floating-point value: -1234.567
'
'Format specifiers only for integral types:
'(D) Decimal:. . . . . . . -12345
'(X) Hexadecimal:. . . . . FFFFCFC7
'
'Format specifier only for the Single and Double types:
'(R) Round-trip: . . . . . -1234.567
'
'Format specifiers for integral or floating-point types:
'(C) Currency: . . . . . . ($1,234.57)
'(E) Scientific: . . . . . -1.234567E+003
'(F) Fixed point:. . . . . -1234.57
'(G) General (default):. . -1234.567
'(N) Number: . . . . . . . -1,234.57
'(P) Percent:. . . . . . . -123,456.70 %
'
'Use ToString(String, IFormatProvider) and a specified culture.
'
'Culture: German (Germany)
'Integral value: -12345
'Floating-point value: -1234.567
'
'Format specifiers only for integral types:
'(D) Decimal:. . . . . . . -12345
'(X) Hexadecimal:. . . . . FFFFCFC7
'
'Format specifier only for the Single and Double types:
'(R) Round-trip: . . . . . -1234,567
'
'Format specifiers for integral or floating-point types:
'(C) Currency: . . . . . . -1.234,57 euro
'(E) Scientific: . . . . . -1,234567E+003
'(F) Fixed point:. . . . . -1234,57
'(G) General (default):. . -1234,567
'(N) Number: . . . . . . . -1.234,57
'(P) Percent:. . . . . . . -123.456,70%
'
// This code example demonstrates the ToString(String) and
// ToString(String, IFormatProvider) methods for integral and
// floating-point numbers, in conjunction with the standard
// numeric format specifiers.
// This code example uses the System.Int32 integral type and
// the System.Double floating-point type, but would yield
// similar results for any of the numeric types. The integral
// numeric types are System.Byte, SByte, Int16, Int32, Int64,
// UInt16, UInt32, and UInt64. The floating-point numeric types
// are Decimal, Single, and Double.
using System;
using System.Globalization;
using System.Threading;
class Sample
{
public static void Main()
{
// Format a negative integer or floating-point number in various ways.
int integralVal = -12345;
double floatingVal = -1234.567d;
string msgCurrency = "(C) Currency: . . . . . . ";
string msgDecimal = "(D) Decimal:. . . . . . . ";
string msgScientific = "(E) Scientific: . . . . . ";
string msgFixedPoint = "(F) Fixed point:. . . . . ";
string msgGeneral = "(G) General (default):. . ";
string msgNumber = "(N) Number: . . . . . . . ";
string msgPercent = "(P) Percent:. . . . . . . ";
string msgRoundTrip = "(R) Round-trip: . . . . . ";
string msgHexadecimal = "(X) Hexadecimal:. . . . . ";
string msg1 = "Use ToString(String) and the current thread culture.\n";
string msg2 = "Use ToString(String, IFormatProvider) and a specified culture.\n";
string msgCulture = "Culture:";
string msgIntegralVal = "Integral value:";
string msgFloatingVal = "Floating-point value:";
CultureInfo ci;
//
Console.Clear();
Console.WriteLine("Standard Numeric Format Specifiers:\n");
// Display the values.
Console.WriteLine(msg1);
// Display the thread current culture, which is used to format the values.
ci = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName);
// Display the integral and floating-point values.
Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal);
Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal);
Console.WriteLine();
// Use the format specifiers that are only for integral types.
Console.WriteLine("Format specifiers only for integral types:");
Console.WriteLine(msgDecimal + integralVal.ToString("D"));
Console.WriteLine(msgHexadecimal + integralVal.ToString("X"));
Console.WriteLine();
// Use the format specifier that is only for the Single and Double
// floating-point types.
Console.WriteLine("Format specifier only for the Single and Double types:");
Console.WriteLine(msgRoundTrip + floatingVal.ToString("R"));
Console.WriteLine();
// Use the format specifiers that are for integral or floating-point types.
Console.WriteLine("Format specifiers for integral or floating-point types:");
Console.WriteLine(msgCurrency + floatingVal.ToString("C"));
Console.WriteLine(msgScientific + floatingVal.ToString("E"));
Console.WriteLine(msgFixedPoint + floatingVal.ToString("F"));
Console.WriteLine(msgGeneral + floatingVal.ToString("G"));
Console.WriteLine(msgNumber + floatingVal.ToString("N"));
Console.WriteLine(msgPercent + floatingVal.ToString("P"));
Console.WriteLine();
// Display the same values using a CultureInfo object. The CultureInfo class
// implements IFormatProvider.
Console.WriteLine(msg2);
// Display the culture used to format the values.
// Create a European culture and change its currency symbol to "euro" because
// this particular code example uses a thread current UI culture that cannot
// display the euro symbol (€).
ci = new CultureInfo("de-DE");
ci.NumberFormat.CurrencySymbol = "euro";
Console.WriteLine("{0,-26}{1}", msgCulture, ci.DisplayName);
// Display the integral and floating-point values.
Console.WriteLine("{0,-26}{1}", msgIntegralVal, integralVal);
Console.WriteLine("{0,-26}{1}", msgFloatingVal, floatingVal);
Console.WriteLine();
// Use the format specifiers that are only for integral types.
Console.WriteLine("Format specifiers only for integral types:");
Console.WriteLine(msgDecimal + integralVal.ToString("D", ci));
Console.WriteLine(msgHexadecimal + integralVal.ToString("X", ci));
Console.WriteLine();
// Use the format specifier that is only for the Single and Double
// floating-point types.
Console.WriteLine("Format specifier only for the Single and Double types:");
Console.WriteLine(msgRoundTrip + floatingVal.ToString("R", ci));
Console.WriteLine();
// Use the format specifiers that are for integral or floating-point types.
Console.WriteLine("Format specifiers for integral or floating-point types:");
Console.WriteLine(msgCurrency + floatingVal.ToString("C", ci));
Console.WriteLine(msgScientific + floatingVal.ToString("E", ci));
Console.WriteLine(msgFixedPoint + floatingVal.ToString("F", ci));
Console.WriteLine(msgGeneral + floatingVal.ToString("G", ci));
Console.WriteLine(msgNumber + floatingVal.ToString("N", ci));
Console.WriteLine(msgPercent + floatingVal.ToString("P", ci));
Console.WriteLine();
}
}
/*
This code example produces the following results:
Standard Numeric Format Specifiers:
Use ToString(String) and the current thread culture.
Culture: English (United States)
Integral value: -12345
Floating-point value: -1234.567
Format specifiers only for integral types:
(D) Decimal:. . . . . . . -12345
(X) Hexadecimal:. . . . . FFFFCFC7
Format specifier only for the Single and Double types:
(R) Round-trip: . . . . . -1234.567
Format specifiers for integral or floating-point types:
(C) Currency: . . . . . . ($1,234.57)
(E) Scientific: . . . . . -1.234567E+003
(F) Fixed point:. . . . . -1234.57
(G) General (default):. . -1234.567
(N) Number: . . . . . . . -1,234.57
(P) Percent:. . . . . . . -123,456.70 %
Use ToString(String, IFormatProvider) and a specified culture.
Culture: German (Germany)
Integral value: -12345
Floating-point value: -1234.567
Format specifiers only for integral types:
(D) Decimal:. . . . . . . -12345
(X) Hexadecimal:. . . . . FFFFCFC7
Format specifier only for the Single and Double types:
(R) Round-trip: . . . . . -1234,567
Format specifiers for integral or floating-point types:
(C) Currency: . . . . . . -1.234,57 euro
(E) Scientific: . . . . . -1,234567E+003
(F) Fixed point:. . . . . -1234,57
(G) General (default):. . -1234,567
(N) Number: . . . . . . . -1.234,57
(P) Percent:. . . . . . . -123.456,70%
*/