天天看點

printf()的格式控制字元

一、printf( )的格式字元:

① d格式符 用來輸出十進制整數

%d 按整型資料的實際長度輸出

讀作mod d。

%md 使輸出長度為m,如果資料長度小于m,則左補空格,如果大于m,則輸出實際長度

%ld 輸出長整型資料

② o格式符 以八進制形式輸出整數

③ x格式符 以十六進制形式輸出整數

④ u格式符 用來輸出unsigned型資料,以十進制形式輸出

⑤ c格式符 用來輸出一個字元

⑥ s格式符 輸出一個字元串

%s 輸出實際長度字元串

%ms 輸出的串占m列,如果串長度小于m,左補空格,如果大于m,實際輸出

%-ms輸出的串占m列,如果串長度小于m,右補空格,

%m.ns 輸出占m列,但隻取字元串中左端n個字元并靠右對齊

%-m.ns m、n含義同上,靠左對齊,如果n>m,則m自動取n值

⑦ f格式符 以小數形式輸出實數

%f 整數部分全部輸出,小數部分輸出6位

%m.nf 輸出資料共占m列,其中有n位小數。如果數值長度小于m,左補空格

%-m.nf 同上,右補空格

⑧ e格式符 以指數形式輸出實數

%e 系統指定6位小數,5位指數(e+002 )

⑨ g格式符 輸出實數,根據數值大小,自動選f格式或e格式

二、JAVA常用的格式控制和轉換字元

3.2.4 輸出格式控制

标準輸出通常是使用指令行模式作為輸出,這裡介紹幾個輸出格式控制技巧,在指令行模式顯示時可以協助控制輸出的格式。首先介紹格式控制字元,常用的控制字元如表3-1所示。

控 制 字 符 作 用

\ 反斜線

\' 單引号'

\" 雙引号"

\uxxxx 以十六進制數指定Unicode字元輸出

\dxxx 以八進制數指定Unicode字元輸出

\b 倒退一個字元

\f 換頁

\n 換行

\r 光标移至行首

\t 跳格(一個Tab鍵)

表3-2 常用格式轉換字元

轉換字元 作 用

%% 在字元串中顯示%

%d 以十進制整數方式輸出,提供的數必須是Byte、Short、 Integer、Long或BigInteger

%f 将浮點數以十進制方式輸出,提供的數必須是Float、Double或 BigDecimal

%e, %E 将浮點數以十進制方式輸出,并使用科學記數法,提供的數必須是Float、 Double或BigDecimal

%a, %A 使用科學記數法輸出浮點數,以十六進制輸出整數部分,以十進制輸出指數部分,提供的數必須Float、Double、

BigDecimal

%o 以八進制整數方式輸出,提供的數必須是Byte、Short、Integer、Long或BigInteger

%x, %X 将浮點數以十六進制方式輸出,提供的數必須是Byte、Short、Integer、Long、或BigInteger

%s, %S 将字元串格式化輸出

%c, %C 以字元方式輸出,提供的數必須是Byte、Short、Character或 Integer

%b, %B 将true或false輸出(或TRUE、FALSE,使用 %B)。另外,非null值輸出是true,null值輸出是false

%t, %T 輸出日期/時間的前置,詳情請看線上API檔案

三、.NET Framework 開發人員指南

标準數字格式字元串

标準數字格式字元串用于格式化通用數值類型。标準數字格式字元串采用 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 對象。

整型和浮點型數值類型

對标準數字格式說明符的一些說明涉及到整型或浮點型數值類型。整型數值類型有 Byte、SByte、Int16、Int32、Int64、UInt16、UInt32 和 UInt64。浮點型數值類型有 Decimal、Single 和 Double。

浮點型無窮大和 NaN

請注意,無論格式字元串原來是什麼值,隻要 Single 或 Double 浮點類型的值為正無窮大、負無窮大或非數字 (NaN),格式字元串就分别是目前适用的 NumberFormatInfo 對象指定的 PositiveInfinitySymbol、NegativeInfinitySymbol 或 NaNSymbol 屬性的值。

示例

下面的代碼示例使用線程目前區域性、指定區域性以及所有标準數字格式說明符對一個整型數值和一個浮點型數值進行格式化。本代碼示例使用兩種特定數值類型,但對于任一基本數值類型(Byte、SByte、Int16、Int32、Int64、UInt16、UInt32、UInt64、Decimal、Single 和 Double)将産生類似的結果。

C#

複制代碼

// 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();

}

}

請參見

參考

NumberFormatInfo

概念

數字格式字元串

标準數字格式字元串輸出示例

自定義數字格式字元串

其他資源

格式化類型

[url]http://msdn.microsoft.com/zh-cn/dwhawy9k(VS.80).aspx[/url]