找回密碼
 立即註冊
搜索
熱搜: 活動 交友 discuz
查看: 500|回復: 1

string.Format

[複製鏈接]

257

主題

38

回帖

1138

積分

管理員

積分
1138
發表於 2023-5-25 09:42:36 | 顯示全部樓層 |閱讀模式

1. 標準數值格式
2. 自訂數值格式
3. 標準DateTime格式
4. 自訂DateTime格式
5. 字串格式

前言

常用到string.Format方法,每次用到都要上網查說明,心想不如整理成文章懶人包方便自己日後查詢,若有錯誤的地方,請各位不吝指教

 

標準數值格式

格式

說明

Format

Input

Output

補充

C貨幣{0:C}1234.567NT$1,234.57Currency C預設到小數2位…C1取小數一位,C3取小數三位…
D十進位{0:D}12341234Decimal 只支援整數資料型別(integral types),D後面數字表示指定的位數
E科學指數{0:E}12341.234000E+003Scientific
F固定{0:F}1234.45671234.46Fixed-point
G一般{0:G}1234.5671234.57General
N數字{0:N}120000120,000.00Number每三位數用 "," 隔開
P百分比{0:P}0.2525.00%Percent輸入數值*100 ; 預設取小數2位,P0可取小數
R來回{0:R}0.250.25Round-trip只支援Double、Single
X十六進位{0:R}1237BHexadecimal:只支援整數資料型別(integral types)


自訂數值格式

格式

說明

Format

Input

Output

補充

0零值預留位置{0:000.000}12.3012.300Zero placeholder
#數字預留位置{0:###.###}12.312.3Digit placeholder
#,,:1234567890→1235
#,,,:1234567890→1
#,##0,,:1234567890→1,235
.小數點{0:0.0}12.312.3Decimal point
,千位分隔符號{0:0,0}12001,200Thousand separator and number scaling
%百分比預留位置{0:0%}0.2525% Percentage placeholder
e科學標記法{0:0e+0}1231e+2Scientific notation
\跳脫字元{0:00\n0}12312 3 Escape character

備註:自訂數值格式化:{0:(###) ### – ####} ,1234567890→(123) 456 – 7890,詳細請參考 自訂數值格式輸出範例


標準DateTime格式

測試時間:2012/3/11 下午 01:02

格式

說明

Format

Output

補充

d簡短日期{0:d}2012/3/11MM/dd/yyyy
D完整日期{0:D}2012年3月11日 
f完整可排序日期/時間{0:f}2012年3月11日 下午 01:02 
F完整可排序日期/時間{0:F}2012年3月11日 下午 01:02:03 
g一般可排序日期/時間{0:g}2012/3/11 下午 01:02 
G一般可排序日期/時間{0:G}2012/3/11 下午 01:02:03 
M、m月日{0:m}3月11日 
o來回日期/時間{0:o}2012-03-11T13:02:03.0000000 
R、rRFC1123{0:R}Sun, 11 Mar 2012 13:02:03 GMT 
s可排序日期/時間{0:s}2012-03-11T13:02:03 
t簡短時間{0:t}下午 01:02HH:mm
T完整時間{0:T}下午 01:02:03HH:mm:ss
u通用可排序日期/時間{0:u}2012-03-11 13:02:03Zyyyy'-'MM'-'dd HH':'mm':'ss'Z'
U通用可排序日期/時間{0:U}2012年3月11日 上午 05:02:03 
Y、y年月{0:y}2012年3月 

備註:輸出日期格式顯示可以依據〈控制台〉 〈地區語言選項〉做修改,詳細請參考:標準DateTime格式輸出範例String.Format yyyy/MM/dd? 誤會大了 by 黑暗大

 

自訂DateTime格式

測試時間:2012/3/11 下午 02:21

格式

說明

Format

Output

補充

dd月份日期{0:dd}11 
ddd星期幾的縮寫{0:ddd}星期日Sun
dddd星期幾的完整名稱{0:dddd}星期日Sunday
f, ff…秒數{0:fff}364 
gg,…時期或時代{0:gg}西元 
hh小時(12 小時制){0:hh}02 
HH小時(24 小時制){0:HH}14 
mm分鐘{0:mm}21 
MM月份{0:MM}03 
MMM月份的縮寫名稱{0:MMM}三月Mar
MMMM月份的完整名稱{0:MMMM}三月March
ss秒數{0:ss}49 
ttA.M./P.M{0:tt}下午 
yy兩個位數的數字來表示年份{0:yy}12 
yyy三個位數的數字來表示年份{0:yyy}2012 
yyyy四個位數的數字來表示年份{0:yyyy}2012 
zz時差(小時){0:zz}+08系統時區與格林威治標準時間 (GMT) 時差
zzz時差(小時&分鐘){0:zzz}+08:00系統時區與格林威治標準時間 (GMT) 時差 (帶正負號)
:時間分隔符號{0:hh:mm:ss}02:29:06 
/日期分隔符號{0:yyyy/MM/dd}2012/03/11 

詳細請參考 自訂DateTime格式字串

 

字串格式


string myBook = "book";
string myPencil = "pencil";
string FormatString = String.Format("This is a {0}, not a {1}", myBook, myPencil);
Response.Write(FormatString); 

Result:This is a book, not a pencil

257

主題

38

回帖

1138

積分

管理員

積分
1138
 樓主| 發表於 2023-5-25 09:42:46 | 顯示全部樓層
//數字字串不足,前面補0
Response.Write(String.Format("{0:00000}", 123)); // 輸出 00123
Response.Write(String.Format("{0:D5}", 123)); // 輸出 00123

//數字字串不足,前後都補0
Response.Write(String.Format("{0:00000.0000}", 123.45)); // 輸出 00125.4500

//每3位數加逗號
Response.Write(String.Format("{0:0,0}", 0)); // 輸出 00
Response.Write(String.Format("{0:0,0}", 1234567)); // 輸出 1,234,567  //缺點:當數字=0時,會顯示 00
Response.Write(String.Format("{0:N}", 1234567)); // 輸出 1,234,567.00
Response.Write(String.Format("{0:N0}", 1234567)); // 輸出 1,234,567
Response.Write(String.Format("{0:N4}", 1234567)); // 輸出 1,234,567.0000

//電話號碼
Response.Write(String.Format("{0:(###) ####-####}", 88012345678)); // 輸出(880)1234-5678

//金額表示方式
Response.Write(String.Format("{0:C}", 0)); // 輸出 NT$0.00
Response.Write(String.Format("{0:C}", 12345)); // 輸出 NT$12,345.00
Response.Write(String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 0)); // 輸出 Zero
Response.Write(String.Format("{0:$#,##0.00;($#,##0.00);Zero}", 1234.50)); // 輸出 $1,234.50

//百分比
Response.Write(String.Format("{0:0%}", 17 / (float)60)); // 輸出 28%

//到小數2位的百分比
Response.Write(String.Format("{0:0.00%}", 17 / (float)60)); // 輸出 28.33%

//取小數第4位,並對第5位做四捨五入
Response.Write(String.Format("{0:#,0.####}", 1234.56789)); // 1,234.5679

//小數點不足4位不補0
Response.Write(String.Format("{0:0.####}", 1234.567)); // 1234.567
您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|DoIT 科技論壇

GMT+8, 2025-6-15 20:13 , Processed in 0.019731 second(s), 18 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回復 返回頂部 返回列表