C# 各种字符串格式
生活随笔
收集整理的這篇文章主要介紹了
C# 各种字符串格式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
C#的String.Format舉例
stringstr1 =string.Format("{0:N1}",56789);??????????????? //result: 56,789.0
stringstr2 =string.Format("{0:N2}",56789);??????????????? //result: 56,789.00
stringstr3 =string.Format("{0:N3}",56789);??????????????? //result: 56,789.000
stringstr8 =string.Format("{0:F1}",56789);??????????????? //result: 56789.0
stringstr9 =string.Format("{0:F2}",56789);??????????????? //result: 56789.00
stringstr11 =(56789 / 100.0).ToString("#.##");??????????? //result: 567.89
stringstr12 =(56789 / 100).ToString("#.##");????????????? //result: 567
C 或 c
貨幣
Console.Write("{0:C}", 2.5);?? //$2.50
Console.Write("{0:C}", -2.5); //($2.50)
D 或 d
十進(jìn)制數(shù)
Console.Write("{0:D5}", 25);?? //00025
E 或 e
科學(xué)型
Console.Write("{0:E}", 250000);?? //2.500000E+005
F 或 f
固定點(diǎn)
Console.Write("{0:F2}", 25);?? //25.00
Console.Write("{0:F0}", 25);?? //25
G 或 g
常規(guī)
Console.Write("{0:G}", 2.5);?? //2.5
N 或 n
數(shù)字
Console.Write("{0:N}", 2500000);?? //2,500,000.00
X 或 x
十六進(jìn)制
Console.Write("{0:X}", 250);
/******************************************************************************/
?? ASP.NET設(shè)置數(shù)據(jù)格式與String.Format使用總結(jié)(引)
{0:d} YY-MM-DD
{0:p} 百分比00.00%
{0:N2} 12.68
{0:N0} 13
{0:c2} $12.68
{0:d} 3/23/2003
{0:T} 12:00:00 AM
{0:男;;女}
DataGrid-數(shù)據(jù)格式設(shè)置表達(dá)式
數(shù)據(jù)格式設(shè)置表達(dá)式
.NET Framework 格式設(shè)置表達(dá)式,它在數(shù)據(jù)顯示在列中之前先應(yīng)用于數(shù)據(jù)。此表達(dá)式由可選靜態(tài)文本和用以下格式表示的格式說明符組成:
{0:format specifier}
零是參數(shù)索引,它指示列中要格式化的數(shù)據(jù)元素;因此,通常用零來指示第一個(且唯一的)元素。format specifier 前面有一個冒號 (:),它由一個或多個字母組成,指示如何格式化數(shù)據(jù)。可以使用的格式說明符取決于要格式化的數(shù)據(jù)類型:日期、數(shù)字或其他類型。下表顯示了不同數(shù)據(jù)類型的格式設(shè)置表達(dá)式的示例。有關(guān)格式設(shè)置表達(dá)式的更多信息,請參見格式化類型。
格式設(shè)置表達(dá)式
應(yīng)用于此數(shù)據(jù)類型
說明
Price: {0:C}
numeric/decimal
顯示“Price:”,后跟以貨幣格式表示的數(shù)字。貨幣格式取決于通過 Page 指令或 Web.config 文件中的區(qū)域性屬性指定的區(qū)域性設(shè)置。
{0:D4}
integer(不能和小數(shù)一起使用。)
在由零填充的四個字符寬的字段中顯示整數(shù)。
{0:N2}%
numeric
顯示精確到小數(shù)點(diǎn)后兩位的數(shù)字,后跟“%”。
{0:000.0}
numeric/decimal
四舍五入到小數(shù)點(diǎn)后一位的數(shù)字。不到三位的數(shù)字用零填充。
{0:D}
date/datetime
長日期格式(“Thursday, August 06, 1996”)。日期格式取決于頁或 Web.config 文件的區(qū)域性設(shè)置。
{0:d}
date/datetime
短日期格式(“12/31/99”)。
{0:yy-MM-dd}
date/datetime
用數(shù)字的年-月-日表示的日期(96-08-06)。
只讀
當(dāng)此列處于編輯模式時,該列中的數(shù)據(jù)是否顯示在可編輯的控件中。
2006-02-22 | asp.net數(shù)據(jù)格式的Format-- DataFormatString
我們在呈現(xiàn)數(shù)據(jù)的時候,不要將未經(jīng)修飾過的數(shù)據(jù)呈現(xiàn)給使用者。例如金額一萬元,如果我們直接顯示「10000」,可能會導(dǎo)致使用者看成一千或十萬,造成使用者閱讀數(shù)據(jù)上的困擾。若我們將一萬元潤飾后輸出為「NT$10,000」,不但讓使比較好閱讀,也會讓使用者減少犯錯的機(jī)會。
下列畫面為潤飾過的結(jié)果:
上述數(shù)據(jù)除了將DataGrid Web 控件以顏色來區(qū)隔記錄外,最主要將日期、單價以及小計這三個計字段的數(shù)據(jù)修飾的更容易閱讀。要修飾字段的輸出,只要設(shè)定字段的DataFormatString 屬性即可;其使用語法如下:
DataFormatString="{0:格式字符串}"
我們知道在DataFormatString 中的 {0} 表示數(shù)據(jù)本身,而在冒號后面的格式字符串代表所們希望數(shù)據(jù)顯示的格式;另外在指定的格式符號后可以指定小數(shù)所要顯示的位數(shù)。例如原來的數(shù)據(jù)為「12.34」,若格式設(shè)定為 {0:N1},則輸出為「12.3」。其常用的數(shù)值格式如下表所示:
格式字符串 資料 結(jié)果
"{0:C}" 12345.6789 $12,345.68
"{0:C}" -12345.6789 ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 00012345
"{0:E}" 12345.6789 1234568E+004
"{0:E10}" 12345.6789 1.2345678900E+004
"{0:F}" 12345.6789 12345.68
"{0:F0}" 12345.6789 12346
"{0:G}" 12345.6789 12345.6789
"{0:G7}" 123456789 1.234568E8
"{0:N}" 12345.6789 12,345.68
"{0:N4}" 123456789 123,456,789.0000
"Total: {0:C}" 12345.6789 Total: $12345.68
其常用的日期格式如下表所示:
格式 說明 輸出格式
d 精簡日期格式 MM/dd/yyyy
D 詳細(xì)日期格式 dddd, MMMM dd, yyyy
f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
F
完整日期時間格式
(long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
m,M 月日格式 MMMM dd
s 適中日期時間格式 yyyy-MM-dd HH:mm:ss
t 精簡時間格式 HH:mm
T 詳細(xì)時間格式 HH:mm:ss
string.format格式結(jié)果
String.Format
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
(G) General:. . . . . . . . . Green
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
說明:
String.Format
將指定的 String 中的每個格式項(xiàng)替換為相應(yīng)對象的值的文本等效項(xiàng)。
例子:
int iVisit = 100;
string szName = "Jackfled";
Response.Write(String.Format("您的帳號是:{0} 。訪問了 {1} 次.", szName, iVisit));
stringstr1 =string.Format("{0:N1}",56789);??????????????? //result: 56,789.0
stringstr2 =string.Format("{0:N2}",56789);??????????????? //result: 56,789.00
stringstr3 =string.Format("{0:N3}",56789);??????????????? //result: 56,789.000
stringstr8 =string.Format("{0:F1}",56789);??????????????? //result: 56789.0
stringstr9 =string.Format("{0:F2}",56789);??????????????? //result: 56789.00
stringstr11 =(56789 / 100.0).ToString("#.##");??????????? //result: 567.89
stringstr12 =(56789 / 100).ToString("#.##");????????????? //result: 567
C 或 c
貨幣
Console.Write("{0:C}", 2.5);?? //$2.50
Console.Write("{0:C}", -2.5); //($2.50)
D 或 d
十進(jìn)制數(shù)
Console.Write("{0:D5}", 25);?? //00025
E 或 e
科學(xué)型
Console.Write("{0:E}", 250000);?? //2.500000E+005
F 或 f
固定點(diǎn)
Console.Write("{0:F2}", 25);?? //25.00
Console.Write("{0:F0}", 25);?? //25
G 或 g
常規(guī)
Console.Write("{0:G}", 2.5);?? //2.5
N 或 n
數(shù)字
Console.Write("{0:N}", 2500000);?? //2,500,000.00
X 或 x
十六進(jìn)制
Console.Write("{0:X}", 250);
/******************************************************************************/
?? ASP.NET設(shè)置數(shù)據(jù)格式與String.Format使用總結(jié)(引)
{0:d} YY-MM-DD
{0:p} 百分比00.00%
{0:N2} 12.68
{0:N0} 13
{0:c2} $12.68
{0:d} 3/23/2003
{0:T} 12:00:00 AM
{0:男;;女}
DataGrid-數(shù)據(jù)格式設(shè)置表達(dá)式
數(shù)據(jù)格式設(shè)置表達(dá)式
.NET Framework 格式設(shè)置表達(dá)式,它在數(shù)據(jù)顯示在列中之前先應(yīng)用于數(shù)據(jù)。此表達(dá)式由可選靜態(tài)文本和用以下格式表示的格式說明符組成:
{0:format specifier}
零是參數(shù)索引,它指示列中要格式化的數(shù)據(jù)元素;因此,通常用零來指示第一個(且唯一的)元素。format specifier 前面有一個冒號 (:),它由一個或多個字母組成,指示如何格式化數(shù)據(jù)。可以使用的格式說明符取決于要格式化的數(shù)據(jù)類型:日期、數(shù)字或其他類型。下表顯示了不同數(shù)據(jù)類型的格式設(shè)置表達(dá)式的示例。有關(guān)格式設(shè)置表達(dá)式的更多信息,請參見格式化類型。
格式設(shè)置表達(dá)式
應(yīng)用于此數(shù)據(jù)類型
說明
Price: {0:C}
numeric/decimal
顯示“Price:”,后跟以貨幣格式表示的數(shù)字。貨幣格式取決于通過 Page 指令或 Web.config 文件中的區(qū)域性屬性指定的區(qū)域性設(shè)置。
{0:D4}
integer(不能和小數(shù)一起使用。)
在由零填充的四個字符寬的字段中顯示整數(shù)。
{0:N2}%
numeric
顯示精確到小數(shù)點(diǎn)后兩位的數(shù)字,后跟“%”。
{0:000.0}
numeric/decimal
四舍五入到小數(shù)點(diǎn)后一位的數(shù)字。不到三位的數(shù)字用零填充。
{0:D}
date/datetime
長日期格式(“Thursday, August 06, 1996”)。日期格式取決于頁或 Web.config 文件的區(qū)域性設(shè)置。
{0:d}
date/datetime
短日期格式(“12/31/99”)。
{0:yy-MM-dd}
date/datetime
用數(shù)字的年-月-日表示的日期(96-08-06)。
只讀
當(dāng)此列處于編輯模式時,該列中的數(shù)據(jù)是否顯示在可編輯的控件中。
2006-02-22 | asp.net數(shù)據(jù)格式的Format-- DataFormatString
我們在呈現(xiàn)數(shù)據(jù)的時候,不要將未經(jīng)修飾過的數(shù)據(jù)呈現(xiàn)給使用者。例如金額一萬元,如果我們直接顯示「10000」,可能會導(dǎo)致使用者看成一千或十萬,造成使用者閱讀數(shù)據(jù)上的困擾。若我們將一萬元潤飾后輸出為「NT$10,000」,不但讓使比較好閱讀,也會讓使用者減少犯錯的機(jī)會。
下列畫面為潤飾過的結(jié)果:
上述數(shù)據(jù)除了將DataGrid Web 控件以顏色來區(qū)隔記錄外,最主要將日期、單價以及小計這三個計字段的數(shù)據(jù)修飾的更容易閱讀。要修飾字段的輸出,只要設(shè)定字段的DataFormatString 屬性即可;其使用語法如下:
DataFormatString="{0:格式字符串}"
我們知道在DataFormatString 中的 {0} 表示數(shù)據(jù)本身,而在冒號后面的格式字符串代表所們希望數(shù)據(jù)顯示的格式;另外在指定的格式符號后可以指定小數(shù)所要顯示的位數(shù)。例如原來的數(shù)據(jù)為「12.34」,若格式設(shè)定為 {0:N1},則輸出為「12.3」。其常用的數(shù)值格式如下表所示:
格式字符串 資料 結(jié)果
"{0:C}" 12345.6789 $12,345.68
"{0:C}" -12345.6789 ($12,345.68)
"{0:D}" 12345 12345
"{0:D8}" 12345 00012345
"{0:E}" 12345.6789 1234568E+004
"{0:E10}" 12345.6789 1.2345678900E+004
"{0:F}" 12345.6789 12345.68
"{0:F0}" 12345.6789 12346
"{0:G}" 12345.6789 12345.6789
"{0:G7}" 123456789 1.234568E8
"{0:N}" 12345.6789 12,345.68
"{0:N4}" 123456789 123,456,789.0000
"Total: {0:C}" 12345.6789 Total: $12345.68
其常用的日期格式如下表所示:
格式 說明 輸出格式
d 精簡日期格式 MM/dd/yyyy
D 詳細(xì)日期格式 dddd, MMMM dd, yyyy
f 完整格式 (long date + short time) dddd, MMMM dd, yyyy HH:mm
F
完整日期時間格式
(long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
g 一般格式 (short date + short time) MM/dd/yyyy HH:mm
G 一般格式 (short date + long time) MM/dd/yyyy HH:mm:ss
m,M 月日格式 MMMM dd
s 適中日期時間格式 yyyy-MM-dd HH:mm:ss
t 精簡時間格式 HH:mm
T 詳細(xì)時間格式 HH:mm:ss
string.format格式結(jié)果
String.Format
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004
(G) General:. . . . . . . . . Green
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
說明:
String.Format
將指定的 String 中的每個格式項(xiàng)替換為相應(yīng)對象的值的文本等效項(xiàng)。
例子:
int iVisit = 100;
string szName = "Jackfled";
Response.Write(String.Format("您的帳號是:{0} 。訪問了 {1} 次.", szName, iVisit));
轉(zhuǎn)載于:https://www.cnblogs.com/swarb/archive/2011/11/17/9924464.html
總結(jié)
以上是生活随笔為你收集整理的C# 各种字符串格式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WinCE启动过程
- 下一篇: C# Replace函数与JS repl