c# 標準數字格式字符串

標準數字格式字符串用於格式化通用數值類型。標準數字格式字符串採用 Axx 的形式,其中 A 是稱爲格式說明符的字母型字符,xx 是稱爲精度說明符的可選整數。精度說明符的範圍從 0 到 99,並且影響結果中的位數。任何包含一個以上字母字符(包括空白)的數字格式字符串都被解釋爲自定義數字格式字符串

下表描述了標準數字格式說明符。有關各格式說明符所產生輸出的示例,請參見標準數字格式字符串輸出示例。有關更多信息,請參見表格之後的備註。

格式說明符 名稱 說明

C 或 c

貨幣

數字轉換爲表示貨幣金額的字符串。轉換由當前 NumberFormatInfo 對象的貨幣格式信息控制。

精度說明符指示所需的小數位數。如果省略精度說明符,則使用當前 NumberFormatInfo 對象給定的默認貨幣精度。

D 或 d

十進制數

只有整型才支持此格式。數字轉換爲十進制數字 (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

常規

根據數字類型以及是否存在精度說明符,數字會轉換爲定點或科學記數法的最緊湊形式。如果精度說明符被省略或爲零,則數字的類型決定默認精度,如下表所示。

  • Byte 或 SByte:3

  • Int16 或 UInt16:5

  • Int32 或 UInt32:10

  • Int64 或 UInt64:19

  • Single:7

  • Double:15

  • Decimal:29

如果用科學記數法表示數字時指數大於 -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 類型支持此格式。往返過程說明符保證轉換爲字符串的數值再次被分析爲相同的數值。使用此說明符格式化數值時,首先使用常規格式對其進行測試:Double 使用 15 位精度,Single 使用 7 位精度。如果此值被成功地分析回相同的數值,則使用常規格式說明符對其進行格式化。但是,如果此值未被成功地分析爲相同數值,則它這樣格式化:Double 使用 17 位精度,Single 使用 9 位精度。

雖然此處可以存在精度說明符,但它將被忽略。使用此說明符時,往返過程優先於精度。

X 或 x

十六進制數

只有整型才支持此格式。數字轉換爲十六進制數字的字符串。格式說明符的大小寫指示對大於 9 的十六進制數字使用大寫字符還是小寫字符。例如,使用“X”產生“ABCDEF”,使用“x”產生“abcdef”。

精度說明符指示結果字符串中所需的最少數字個數。如果需要的話,則用零填充該數字的左側,以產生精度說明符給定的數字個數。

任何其他單個字符

(未知說明符)

(未知說明符將引發運行庫格式異常。)

備註

控制面板設置

控制面板中“區域和語言選項”項中的設置會影響由格式化操作產生的結果字符串。這些設置用於初始化與當前線程區域性關聯的 NumberFormatInfo 對象,並且當前線程區域性將提供用於控制格式設置的值。使用不同設置的計算機會生成不同的結果字符串。

NumberFormatInfo 屬性

格式化受當前 NumberFormatInfo 對象的屬性影響,其由當前線程區域性隱式提供或由調用格式化的方法的 IFormatProvider 參數顯示提供。爲該參數指定 NumberFormatInfo 或 CultureInfo 對象。

整型和浮點型數值類型

對標準數字格式說明符的一些說明涉及到整型或浮點型數值類型。整型數值類型有 ByteSByteInt16Int32Int64UInt16UInt32 和 UInt64。浮點型數值類型有 DecimalSingle 和 Double

浮點型無窮大和 NaN

請注意,無論格式字符串原來是什麼值,只要 Single 或 Double 浮點類型的值爲正無窮大、負無窮大或非數字 (NaN),格式字符串就分別是當前適用的 NumberFormatInfo 對象指定的 PositiveInfinitySymbolNegativeInfinitySymbol 或 NaNSymbol 屬性的值。

示例

下面的代碼示例使用線程當前區域性、指定區域性以及所有標準數字格式說明符對一個整型數值和一個浮點型數值進行格式化。本代碼示例使用兩種特定數值類型,但對於任一基本數值類型(ByteSByteInt16Int32Int64UInt16UInt32UInt64DecimalSingle 和 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.



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%



*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章