ASP.NET設定資料格式與String.Format使用總結

iDotNetSpace發表於2009-01-12

{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-資料格式設定表示式
這裡需要注意以下幾點
1. 在GridView中的asp:BoundField使用DataFormatString必須設定屬性HtmlEncode="False",否則不起作用。
2. 如果需要使用日期型別的格式化字串,必須資料實體中對應的欄位也應該日起型別的。
3. 格式化字串C代表貨幣單位,需要繫結的資料型別應該是數字型別的。如果是字串型別的不起作用,需要手動新增格式化字串為DataFormatString="{0:C}"

總結:
       GridView中使用DataFromatString與在DataGrid中使用起來有些不同的!在GridView中的BoundField新增了HtmlEncode 屬性,且預設是true,這就使得DataFromatString失效!

資料格式設定表示式
.NET Framework 格式設定表示式,它在資料顯示在列中之前先應用於資料。此表示式由可選靜態文字和用以下格式表示的格式說明符組成: {0:format specifier}

零是引數索引,它指示列中要格式化的資料元素;因此,通常用零來指示第一個(且唯一的)元素。format specifier 前面有一個冒號 (:),它由一個或多個字母組成,指示如何格式化資料。可以使用的格式說明符取決於要格式化的資料型別:日期、數字或其他型別。

格式設定表示式
應用於此資料型別
說明
Price: {0:C}
numeric/decimal
顯示“Price:”,後跟以貨幣格式表示的數字。貨幣格式取決於通過 Page 指令或 Web.config 檔案中的區域性屬性指定的區域性設定。
{0:D4}
integer(不能和小數一起使用。)
在由零填充的四個字元寬的欄位中顯示整數。
{0:N2}%
numeric
顯示精確到小數點後兩位的數字,後跟“%”
{0:000.0}
numeric/decimal
四捨五入到小數點後一位的數字。不到三位的數字用零填充。
{0:D}
date/datetime
長日期格式(“Thursday, August 06, 1996”)。日期格式取決於頁或 Web.config 檔案的區域性設定。
{0:d}
date/datetime
短日期格式(“12/31/99”)。
{0:yy-MM-dd}
date/datetime
用數字的年-月-日表示的日期(96-08-06)。

我們在呈現資料的時候,不要將未經修飾過的資料呈現給使用者。例如金額一萬元,如果我們直接顯示「10000」,可能會導致使用者看成一千或十萬,造成使用者閱讀資料上的困擾。若我們將一萬元潤飾後輸出為「NT$10,000」,不但讓使比較好閱讀,也會讓使用者減少犯錯的機會。

要修飾欄位的輸出,只要設定欄位的DataFormatString 屬性即可;其使用語法如下:DataFormatString="{0:格式字串}"

我們知道在DataFormatString 中的 {0} 表示資料本身,而在冒號後面的格式字串代表所們希望資料顯示的格式;另外在指定的格式符號後可以指定小數所要顯示的位數。例如原來的資料為「12.34」,若格式設定為 {0:N1},則輸出為「12.3」。其常用的數值格式如下表所示:

格式字串 資料 結果
"{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 詳細日期格式 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 詳細時間格式 HH:mm:ss

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 中的每個格式項替換為相應物件的值的文字等效項。

例子:
int iVisit = 100;
string szName = "Jackfled";
Response.Write(String.Format("您的帳號是:{0} 。訪問了 {1} 次.", szName, iVisit));

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-536498/,如需轉載,請註明出處,否則將追究法律責任。

相關文章