C# 格式化字符串 String.Format
格式化日期和數(shù)字的字符串經(jīng)常要用到這個(gè), 就把幫助里面的東西大概整理了一些列在這里了.
下表描述了用來(lái)格式化 DateTime 對(duì)象的標(biāo)準(zhǔn)格式說(shuō)明符。
格式說(shuō)明符 名稱 說(shuō)明
d 短日期模式 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.ShortDatePattern 屬性定義的模式或者由指定格式提供程序定義的模式。
D 長(zhǎng)日期模式 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.LongDatePattern 屬性定義的模式或者由指定格式提供程序定義的模式。
t 短時(shí)間模式 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.ShortTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。
T 長(zhǎng)時(shí)間模式 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.LongTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。
f 完整日期/時(shí)間模式(短時(shí)間) 顯示長(zhǎng)日期和短時(shí)間模式的組合,由空格分隔。
F 完整日期/時(shí)間模式(長(zhǎng)時(shí)間) 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.FullDateTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。
g 常規(guī)日期/時(shí)間模式(短時(shí)間) 顯示短日期和短時(shí)間模式的組合,由空格分隔。
G 常規(guī)日期/時(shí)間模式(長(zhǎng)時(shí)間) 顯示短日期和長(zhǎng)時(shí)間模式的組合,由空格分隔。
M 或 m 月日模式 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.MonthDayPattern 屬性定義的模式或者由指定格式提供程序定義的模式。
R 或 r RFC1123 模式 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.RFC1123Pattern 屬性定義的模式或者由指定格式提供程序定義的模式。這是定義的標(biāo)準(zhǔn),并且屬性是只讀的;因此,無(wú)論所使用的區(qū)域性或所提供的格式提供程序是什么,它總是相同的。屬性引用 CultureInfo.InvariantCulture 屬性并遵照自定義模式“ddd, dd MMM yyyy HH:mm:ss G\MT”。請(qǐng)注意,“GMT”中的“M”需要轉(zhuǎn)義符,因此它不被解釋。格式化并不修改 DateTime 的值,所以您必須在格式化之前將值調(diào)整為 GMT。
s 可排序的日期/時(shí)間模式;符合 ISO 8601 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.SortableDateTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。屬性引用 CultureInfo.InvariantCulture 屬性,格式遵照自定義模式“yyyy-MM-ddTHH:mm:ss”。
u 通用的可排序日期/時(shí)間模式 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.UniversalSortableDateTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。因?yàn)樗嵌x的標(biāo)準(zhǔn),并且屬性是只讀的,因此無(wú)論區(qū)域性或格式提供程序是什么,模式總是相同的。格式化遵照自定義模式“yyyy-MM-dd HH:mm:ssZ”。格式化日期和時(shí)間時(shí)不進(jìn)行時(shí)區(qū)轉(zhuǎn)換;所以,請(qǐng)?jiān)谑褂酶袷秸f(shuō)明符之前將本地日期和時(shí)間轉(zhuǎn)換為通用時(shí)間。
U 通用的可排序日期/時(shí)間模式 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.FullDateTimePattern 屬性定義的模式或者由指定格式提供程序定義的模式。請(qǐng)注意,顯示的時(shí)間是通用時(shí)間,而不是本地時(shí)間。
Y 或 y 年月模式 顯示由與當(dāng)前線程關(guān)聯(lián)的 DateTimeFormatInfo.YearMonthPattern 屬性定義的模式或者由指定格式提供程序定義的模式。
任何其他單個(gè)字符 未知說(shuō)明符
下表描述了標(biāo)準(zhǔn)數(shù)字格式字符串。請(qǐng)注意,這些格式說(shuō)明符產(chǎn)生的輸出字符串受“區(qū)域選項(xiàng)”控制面板中的設(shè)置的影響。使用不同設(shè)置的計(jì)算機(jī)會(huì)生成不同的輸出字符串。
格式說(shuō)明符 名稱 說(shuō)明
C 或 c 貨幣 數(shù)字轉(zhuǎn)換為表示貨幣金額的字符串。轉(zhuǎn)換由用于格式化數(shù)字的 NumberFormatInfo 對(duì)象的貨幣格式信息控制。精度說(shuō)明符指示所需的小數(shù)位數(shù)。如果省略精度說(shuō)明符,則使用 NumberFormatInfo 給定的默認(rèn)貨幣精度。
D 或 d 十進(jìn)制 只有整型才支持此格式。數(shù)字轉(zhuǎn)換為十進(jìn)制數(shù)字 (0-9) 的字符串,如果數(shù)字為負(fù),則前面加負(fù)號(hào)。精度說(shuō)明符指示結(jié)果字符串中所需的最少數(shù)字個(gè)數(shù)。如果需要的話,則用零填充該數(shù)字的左側(cè),以產(chǎn)生精度說(shuō)明符給定的數(shù)字個(gè)數(shù)。
E 或 e 科學(xué)計(jì)數(shù)法(指數(shù)) 數(shù)字轉(zhuǎn)換為“-d.ddd...E+ddd”或“-d.ddd...e+ddd”形式的字符串,其中每個(gè)“d”表示一個(gè)數(shù)字 (0-9)。如果該數(shù)字為負(fù),則該字符串以減號(hào)開頭。小數(shù)點(diǎn)前總有一個(gè)數(shù)字。精度說(shuō)明符指示小數(shù)點(diǎn)后所需的位數(shù)。如果省略精度說(shuō)明符,則使用默認(rèn)值,即小數(shù)點(diǎn)后六位數(shù)字。格式說(shuō)明符的大小寫指示在指數(shù)前加前綴“E”還是“e”。指數(shù)總是由正號(hào)或負(fù)號(hào)以及最少三位數(shù)字組成。如果需要,用零填充指數(shù)以滿足最少三位數(shù)字的要求。
F 或 f 固定點(diǎn) 數(shù)字轉(zhuǎn)換為“-ddd.ddd...”形式的字符串,其中每個(gè)“d”表示一個(gè)數(shù)字 (0-9)。如果該數(shù)字為負(fù),則該字符串以減號(hào)開頭。精度說(shuō)明符指示所需的小數(shù)位數(shù)。如果忽略精度說(shuō)明符,則使用 NumberFormatInfo 給定的默認(rèn)數(shù)值精度。
G 或 g 常規(guī) 根據(jù)數(shù)字類型以及是否存在精度說(shuō)明符,數(shù)字會(huì)轉(zhuǎn)換為固定點(diǎn)或科學(xué)記數(shù)法的最緊湊形式。如果精度說(shuō)明符被省略或?yàn)榱?#xff0c;則數(shù)字的類型決定默認(rèn)精度,如下表所示。
Byte 或 SByte:3
Int16 或 UInt16:5
Int32 或 UInt32:10
Int64 或 UInt64:19
Single:7
Double:15
Decimal:29
如果用科學(xué)記數(shù)法表示數(shù)字時(shí)指數(shù)大于 -5 而且小于精度說(shuō)明符,則使用固定點(diǎn)表示法;否則使用科學(xué)記數(shù)法。如果要求有小數(shù)點(diǎn),并且忽略尾部零,則結(jié)果包含小數(shù)點(diǎn)。如果精度說(shuō)明符存在,并且結(jié)果的有效數(shù)字位數(shù)超過指定精度,則通過舍入刪除多余的尾部數(shù)字。使用科學(xué)記數(shù)法時(shí),如果格式說(shuō)明符是“G”,結(jié)果的指數(shù)帶前綴“E”;如果格式說(shuō)明符是“g”,結(jié)果的指數(shù)帶前綴“e”。
上述規(guī)則有一個(gè)例外:如果數(shù)字是 Decimal 而且省略精度說(shuō)明符時(shí)。在這種情況下總使用固定點(diǎn)表示法并保留尾部零。
N 或 n 數(shù)字 數(shù)字轉(zhuǎn)換為“-d,ddd,ddd.ddd...”格式的字符串,其中每個(gè)“d”表示一個(gè)數(shù)字 (0-9)。如果該數(shù)字為負(fù),則該字符串以減號(hào)開頭。小數(shù)點(diǎn)左邊每三個(gè)數(shù)字之間插入一個(gè)千位分隔符。精度說(shuō)明符指示所需的小數(shù)位數(shù)。如果忽略精度說(shuō)明符,則使用 NumberFormatInfo 給定的默認(rèn)數(shù)值精度。
P 或 p 百分比 數(shù)字轉(zhuǎn)換為由 NumberFormatInfo.PercentNegativePattern 屬性或 NumberFormatInfo.PercentPositivePattern 屬性定義的、表示百分比的字符串。如果數(shù)字為負(fù),則產(chǎn)生的字符串由 PercentNegativePattern 定義并以負(fù)號(hào)開頭。已轉(zhuǎn)換的數(shù)字乘以 100 以表示為百分比。精度說(shuō)明符指示所需的小數(shù)位數(shù)。如果省略精度說(shuō)明符,則使用 NumberFormatInfo 給定的默認(rèn)數(shù)值精度。
R 或 r 往返過程 往返過程說(shuō)明符保證轉(zhuǎn)換為字符串的數(shù)值再次被分析為相同的數(shù)值。使用此說(shuō)明符格式化數(shù)值時(shí),首先用常規(guī)格式測(cè)試:Double 使用 15 位精度,Single 使用 7 位精度。如果此值被成功地分析回相同的數(shù)值,則使用常規(guī)格式說(shuō)明符對(duì)其進(jìn)行格式化。但是,如果此值未被成功地分析為相同的數(shù)值,則它這樣格式化:Double 使用 17 位精度,Single 使用 9 位精度。雖然精度說(shuō)明符可以追加到往返過程格式說(shuō)明符,但它將被忽略。使用此說(shuō)明符時(shí),往返過程優(yōu)先于精度。此格式僅受浮點(diǎn)型支持。
X 或 x 十六進(jìn)制 數(shù)字轉(zhuǎn)換為十六進(jìn)制數(shù)字的字符串。格式說(shuō)明符的大小寫指示對(duì)大于 9 的十六進(jìn)制數(shù)字使用大寫字符還是小寫字符。例如,使用“X”產(chǎn)生“ABCDEF”,使用“x”產(chǎn)生“abcdef”。精度說(shuō)明符指示結(jié)果字符串中所需的最少數(shù)字個(gè)數(shù)。如果需要的話,則用零填充該數(shù)字的左側(cè),以產(chǎn)生精度說(shuō)明符給定的數(shù)字個(gè)數(shù)。只有整型才支持此格式。
如果標(biāo)準(zhǔn)數(shù)字格式說(shuō)明符未提供所需的格式化類型,可以使用自定義格式字符串進(jìn)一步增強(qiáng)字符串輸出。標(biāo)準(zhǔn)格式字符串包含一個(gè)字母字符,后面可能會(huì)跟有數(shù)字序列(形成一個(gè) 0 到 99 的值);而所有其他格式字符串都是自定義格式字符串。
下表顯示可以用于創(chuàng)建自定義數(shù)字格式字符串及其定義的字符。請(qǐng)注意,與當(dāng)前線程關(guān)聯(lián)的 NumberFormatInfo 對(duì)象的“區(qū)域選項(xiàng)”控制面板的設(shè)置會(huì)影響這些字符中的某些所產(chǎn)生的輸出字符串。使用不同區(qū)域性的計(jì)算機(jī)將生成不同的輸出字符串。
格式字符值說(shuō)明
0零占位符如果格式化的值在格式字符串中出現(xiàn)“0”的位置有一個(gè)數(shù)字,則此數(shù)字被復(fù)制到輸出字符串中。小數(shù)點(diǎn)前最左邊的“0”的位置和小數(shù)點(diǎn)后最右邊的“0”的位置確定總在輸出字符串中出現(xiàn)的數(shù)字范圍。“00”說(shuō)明符使得值被舍入到小數(shù)點(diǎn)前最近的數(shù)字,其中零位總被舍去。例如,用“00”格式化 34.5 將得到值 35。
#數(shù)字占位符如果格式化的值在格式字符串中出現(xiàn)“#”的位置有一個(gè)數(shù)字,則此數(shù)字被復(fù)制到輸出字符串中。否則,輸出字符串中的此位置不存儲(chǔ)任何值。請(qǐng)注意,如果“0”不是有效數(shù)字,此說(shuō)明符永不顯示“0”字符,即使“0”是字符串中唯一的數(shù)字。如果“0”是所顯示的數(shù)字中的有效數(shù)字,則顯示“0”字符。“##”格式字符串使得值被舍入到小數(shù)點(diǎn)前最近的數(shù)字,其中零總被舍去。例如,用“##”格式化 34.5 將得到值 35。
.小數(shù)點(diǎn)格式字符串中的第一個(gè)“.”字符確定格式化的值中的小數(shù)點(diǎn)分隔符的位置;任何其他“.”字符被忽略。用作小數(shù)點(diǎn)分隔符的實(shí)際字符由控制格式化的 NumberFormatInfo 的 NumberDecimalSeparator 屬性確定。
,千位分隔符和數(shù)字比例換算“,”字符有兩種用途。首先,如果格式字符串在小數(shù)點(diǎn)(如果有)左邊的兩個(gè)數(shù)字占位符(0 或 #)之間包含“,”字符,則輸出將在小數(shù)點(diǎn)分隔符左邊的每三個(gè)數(shù)字之間插入千位分隔符。輸出字符串中用作小數(shù)點(diǎn)分隔符的實(shí)際字符由控制格式化的當(dāng)前 NumberFormatInfo 的 NumberGroupSeparator 屬性確定。
其次,如果格式字符串在緊鄰小數(shù)點(diǎn)的左側(cè)包含一個(gè)或多個(gè)“,”字符,則數(shù)字在格式化之前將被“,”字符數(shù)除然后乘以 1000。例如,格式字符串“0,,”將 100,000,000 簡(jiǎn)單表示為 100。使用“,”字符指示比例換算在格式化數(shù)字中不包括千位分隔符。因此,若要將數(shù)字縮小 1,000,000 倍并插入千位分隔符,應(yīng)使用格式字符串“#,##0,,”。
%百分比占位符在格式字符串中出現(xiàn)“%”字符將導(dǎo)致數(shù)字在格式化之前乘以 100。適當(dāng)?shù)姆?hào)插入到數(shù)字本身在格式字符串中出現(xiàn)“%”的位置。使用的百分比字符由當(dāng)前的 NumberFormatInfo 類確定。
E0
E+0
E-0
e0
e+0
e-0科學(xué)計(jì)數(shù)法如果“E”、“E+”、“E-”、“e”、“e+”或“e-”中的任何一個(gè)字符串出現(xiàn)在格式字符串中,而且后面緊跟至少一個(gè)“0”字符,則數(shù)字用科學(xué)計(jì)數(shù)法來(lái)格式化,在數(shù)字和指數(shù)之間插入“E”或“e”。跟在科學(xué)計(jì)數(shù)法指示符后面的“0”字符數(shù)確定指數(shù)輸出的最小位數(shù)。“E+”和“e+”格式指示符號(hào)字符(正號(hào)或負(fù)號(hào))應(yīng)總是置于指數(shù)前面。“E”、“E-”、“e”或“e-”格式指示符號(hào)字符僅置于負(fù)指數(shù)前面。
\轉(zhuǎn)義符在 C# 和 C++ 的托管擴(kuò)展中,反斜杠字符使格式字符串中的下一個(gè)字符被解釋為轉(zhuǎn)義序列。它與傳統(tǒng)的格式化序列一起使用,如“\n”(換行)。
在某些語(yǔ)言中,轉(zhuǎn)義符本身用作文本時(shí)必須跟在轉(zhuǎn)義符之后。否則,編譯器將該字符理解為轉(zhuǎn)義符。使用字符串“\\”顯示“\”。
請(qǐng)注意,Visual Basic 中不支持此轉(zhuǎn)義符,但是 ControlChars 提供相同的功能。
'ABC'
"ABC"字符串引在單引號(hào)或雙引號(hào)中的字符被原樣復(fù)制到輸出字符串中,而且不影響格式化。
;部分分隔符“;”字符用于分隔格式字符串中的正數(shù)、負(fù)數(shù)和零各部分。
其他所有其他字符所有其他字符以文本形式復(fù)制到輸出字符串中它們出現(xiàn)的位置。
請(qǐng)注意,對(duì)于固定點(diǎn)格式字符串(不包含“E0”、“E+0”、“E-0”、“e0”、“e+0”或“e-0”的字符串),數(shù)字被舍入為與小數(shù)點(diǎn)右邊的數(shù)字占位符數(shù)目相同的小數(shù)位數(shù)。如果格式字符串不包含小數(shù)點(diǎn),數(shù)字被舍入為最接近的整數(shù)。如果數(shù)字位數(shù)多于小數(shù)點(diǎn)左邊數(shù)字占位符的個(gè)數(shù),多余的數(shù)字被復(fù)制到輸出字符串中緊挨著第一個(gè)數(shù)字占位符的前面。
可以根據(jù)值為正、為負(fù)還是為零來(lái)為字符串應(yīng)用不同的格式化。為產(chǎn)生這種行為,自定義格式字符串可以包含最多三個(gè)用分號(hào)分隔的部分:
一個(gè)部分:格式字符串應(yīng)用于所有值。
兩個(gè)部分:第一部分應(yīng)用于正值和零,第二部分應(yīng)用于負(fù)值。如果要格式化的數(shù)字為負(fù),但根據(jù)第二部分中的格式舍入后為零,則最終的零根據(jù)第一部分進(jìn)行格式化。
三個(gè)部分:第一部分應(yīng)用于正值,第二部分應(yīng)用于負(fù)值,第三部分應(yīng)用于零。第二部分可能為空(分號(hào)間沒有任何內(nèi)容),在這種情況下,第一部分應(yīng)用于所有非零值。如果要格式化的數(shù)字為非零值,但根據(jù)第一部分或第二部分中的格式舍入后為零,則最終的零根據(jù)第三部分進(jìn)行格式化。
格式化最終值時(shí),此類型的格式化忽略所有先前存在的與數(shù)字關(guān)聯(lián)的格式化。例如,使用部分分隔符時(shí),顯示的負(fù)值永遠(yuǎn)不帶負(fù)號(hào)。如果您希望格式化后的最終值帶有負(fù)號(hào),則應(yīng)明確包含負(fù)號(hào),讓它作為自定義格式說(shuō)明符的組成部分。下面的示例闡釋如何使用部分分隔符產(chǎn)生格式化字符串。
[Visual Basic]
Dim MyPos As Double = 19.95
Dim MyNeg As Double = -19.95
Dim MyZero As Double = 0
Dim MyString As String = MyPos.ToString("$#,##0.00;($#,##0.00);Zero")
' In the U.S. English culture, MyString has the value: $19.95.
MyString = MyNeg.ToString("$#,##0.00;($#,##0.00);Zero")
' In the U.S. English culture, MyString has the value: ($19.95).
' The minus sign is omitted by default.
MyString = MyZero.ToString("$#,##0.00;($#,##0.00);Zero")
' In the U.S. English culture, MyString has the value: Zero.
[C#]
double MyPos = 19.95, MyNeg = -19.95, MyZero = 0.0;
string MyString = MyPos.ToString("$#,##0.00;($#,##0.00);Zero");
// In the U.S. English culture, MyString has the value: $19.95.
MyString = MyNeg.ToString("$#,##0.00;($#,##0.00);Zero");
// In the U.S. English culture, MyString has the value: ($19.95).
// The minus sign is omitted by default.
MyString = MyZero.ToString("$#,##0.00;($#,##0.00);Zero");
// In the U.S. English culture, MyString has the value: Zero.
下面的示例說(shuō)明自定義數(shù)字格式化。
[Visual Basic]
Dim myDouble As Double = 1234567890
Dim myString As String = myDouble.ToString( "(###) ### - ####" )
' The value of myString is "(123) 456 – 7890".
Dim MyInt As Integer = 42
MyString = MyInt.ToString( "My Number " + ControlChars.Lf + "= #" )
' In the U.S. English culture, MyString has the value:
' "My Number
' = 42".
[C#]
Double myDouble = 1234567890;
String myString = myDouble.ToString( "(###) ### - ####" );
// The value of myString is "(123) 456 – 7890".
int MyInt = 42;
MyString = MyInt.ToString( "My Number \n= #" );
// In the U.S. English culture, MyString has the value:
// "My Number
// = 42".
------------------------------------------------------------------------------------------------
http://msdn.microsoft.com/library/dotnet/cpguide/cpconstandardnumericformatstrings.htm(這個(gè)是連接url)
Standard Numeric Format Strings are used to return commonly used numeric string types. They take the form X0, where X is the format specifier and 0 is the precision specifier. The format specifier can be one of the seven built-in format characters that define the most commonly used .NET Framework numeric format types. The precision specifier controls the number of significant digits or zeros to the right of a decimal.
Format character Description Default return format (without precision specifier)
C or c Currency format $XX,XX.XX
($XX,XXX.XX)
D or d Decimal format [-]XXXXXXX
E or e Scientific (exponential) format [-]X.XXXXXXE+xxx
[-]X.XXXXXXe+xxx
[-]X.XXXXXXE-xxx
[-]X.XXXXXXe-xxx
F or f Fixed-point format [-]XXXXXXX.XX
G or g General format Variable. Either general or scientific.
N or n Number format [-]XX,XXX.XX
X or x Hexadecimal format Variable. Returns the minimum hexadecimal representation.??
Currency Format
The "C" format specifier causes the Format method to return a string representing the number as a currency value. The currency symbols used (currency symbol, decimal separator, group separator, and so on) are determined by the current culture if a NumberFormatInfo object is not provided. An integer following the "C" determines the number of decimal places that are displayed. If no number is provided, two digits are shown after the decimal separator.
[C#]
int MyInt = 12345;
MyInt.format( "c", null );
// Returns the currency string "$12,345.00"
MyInt.format( "c3", null );
// Returns the exponential string "$12,345.000"
Decimal Format
The "D" format specifier converts the value from an integer to a base 10 (decimal) number. Only integral types support the "D" format code. A negative symbol is prefixed to the result if the value is negative. You can also specify the minimum number of decimal digits to display using a precision specifier.
[C#]
int MyInt = 123;
MyInt.format( "d5", null );
// Returns the decimal string "00123"
MyInt.format( "d2", null );
// Returns the decimal string "123"
Exponential Format
The “E” format specifier causes the Format method to return a string formatted as a scientific (or exponential) number. The precision specifier determines the number of digits after the decimal point. The case of the exponent format character ("E" or "e") determines the case of the exponent symbol.
[C#]
int MyInt = 12345;
MyInt.format( "e", null );
// Returns the exponential string "1.234500e+004"
MyInt.format( "e3", null );
// Returns the exponential string "1.235e+004"
Fixed Point Format
The "F" format specifier inserts a decimal to the right of a nondecimal number followed by the number of zeros specified by the precision specifier. If no precision specifier is supplied, two zeros will be inserted.
[C#]
int MyInt = 12345;
MyInt.format( "f", null );
// Returns the exponential string "12345.00"
MyInt.format( "f3", null );
// Returns the exponential string "12345.000"
General Format
The "G" format specifier converts a numeric value to either fixed point ("F") or scientific format ("E"). This specifier returns the most compact string representation for a given number.
[C#]
int MyInt = 12345;
MyInt.format( "g", null );
// Returns the exponential string "12345"
MyInt.format( "g3", null );
// Returns the exponential string "123e4"
Number Format
The "N" format specifier converts a numeric value to the form "[-]d,ddd,ddd.dd". A decimal is inserted at the far right of the number followed by the number of zeros specified by the format specifier. If no precision specifier is supplied, two zeros are inserted.
[C#]
int MyInt = 12345;
MyInt.format( "n", null );
// Returns the exponential string "12,345.00"
MyInt.format( "n3", null );
// Returns the exponential string "12,345.000"
Hexadecimal Format
The "X" format specifier converts a numeric value to a hexadecimal (base 16) string representation. The precision specifier determines the minimum number of digits returned. If no precision specifier is supplied, the minimum number of digits needed to represent the value is returned; otherwise, the number will be padded with zeros to meet the number of digits required by the precision specifier.
[C#]
int MyInt = 12345;
MyInt.format( "x", null );
// Returns the exponential string "3039"
MyInt.format( "x3", null );
// Returns the exponential string "3039"
轉(zhuǎn)載于:https://www.cnblogs.com/reommmm/articles/1130902.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的C# 格式化字符串 String.Format的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Permute 3 for mac(全能
- 下一篇: C#FTP操作