一、字符串的格式化輸出
1.1 格式化輸出表
字符 |
說明 |
示例 |
輸出 |
C |
貨幣 |
string.Format("{0:C3}", 2) |
$2.000 |
D |
十進制 |
string.Format("{0:D3}", 2) |
002 |
E |
科學計數法 |
string.Format(“{0:e}”,1.2) |
1.20E+001 |
G |
常規 |
string.Format("{0:G}", 2) |
2 |
N |
用分號隔開的數字 |
string.Format("{0:N}", 250000) |
250,000.00 |
X |
十六進制 |
string.Format("{0:X000}", 12) |
C |
string.Format("{0:000.000}", 12.2) |
012.200 |
eg.數字的格式指定
可以通過往剛才所說的修飾項目中添加幾個參數來實現控制數字格式的目的。下表列出了幾個常見的例子。
操作格式 | 代碼 |
輸出 |
固定寬度右對齊 | String.Format("{0, 4}", num) |
“ |
固定寬度左對齊 | String.Format("{0, -4}", num) |
“1 ” |
用0填充 | String.Format("{0:D4}", num) 或者 String.Format("{0:0000}", num) |
“0001” |
固定寬度並用0填充 | String.Format("{0, 8:D4}", num) |
“ 0001” |
1.2 Strings
There really isn't any formatting within a strong, beyond it's alignment. Alignment works for any argument being printed in a String.Format call.
Sample |
Generates |
String.Format("->{1,10}<-", "Hello"); |
-> Hello<- |
String.Format("->{1,-10}<-", "Hello"); |
->Hello <- |
1.3 Numbers
Basic number formatting specifiers:
Specifier |
Type |
Format |
Output |
Output |
c |
Currency |
{0:c} |
$1.42 |
-$12,400 |
d |
Decimal (Whole number) |
{0:d} |
System. |
-12400 |
e |
Scientific |
{0:e} |
1.420000e+000 |
-1.240000e+004 |
f |
Fixed point |
{0:f} |
1.42 |
-12400.00 |
g |
General |
{0:g} |
1.42 |
-12400 |
n |
Number with commas for thousands |
{0:n} |
1.42 |
-12,400 |
r |
Round trippable |
{0:r} |
1.42 |
System. |
x |
Hexadecimal |
{0:x4} |
System. |
cf90 |
Custom number formatting ( Passed Double 1500.42 ) :
Specifier |
Type |
Example |
Output |
Note |
0 |
Zero placeholder |
{0:00.0000} |
1500.4200 |
Pads with zeroes. |
# |
Digit placeholder |
{0:(#).##} |
(1500).42 |
|
. |
Decimal point |
{0:0.0} |
1500.4 |
|
, |
Thousand separator |
{0:0,0} |
1,500 |
Must be between two zeroes. |
,. |
Number scaling |
{0:0,.} |
2 |
Comma adjacent to Period scales by 1000. |
% |
Percent |
{0:0%} |
150042% |
Multiplies by 100, adds % sign. |
e |
Exponent placeholder |
{0:00e+0} |
15e+2 |
Many exponent formats available. |
; |
Group separator |
see below |
|
The group separator is especially useful for formatting currency values which require that negative values be enclosed in parentheses. This currency formatting example at the bottom of this document makes it obvious:
1.4 Dates
Note that date formatting is especially dependant on the system's regional settings; the example strings here are from my local locale.
Specifier |
Type |
Example (Passed System.DateTime.Now) |
d |
Short date |
10/12/2002 |
D |
Long date |
December 10, 2002 |
t |
Short time |
10:11 PM |
T |
Long time |
10:11:29 PM |
f |
Full date & time |
December 10, 2002 10:11 PM |
F |
Full date & time (long) |
December 10, 2002 10:11:29 PM |
g |
Default date & time |
10/12/2002 10:11 PM |
G |
Default date & time (long) |
10/12/2002 10:11:29 PM |
M |
Month day pattern |
December 10 |
r |
RFC1123 date string |
Tue, 10 Dec 2002 22:11:29 GMT |
s |
Sortable date string |
2002-12-10T22:11:29 |
u |
Universal sortable, local time |
2002-12-10 22:13:50Z |
U |
Universal sortable, GMT |
December 11, 2002 3:13:50 AM |
Y |
Year month pattern |
December, 2002 |
The 'U' specifier seems broken; that string certainly isn't sortable.
Custom date formatting:
Specifier |
Type |
Example |
Example Output |
dd |
Day |
{0:dd} |
10 |
ddd |
Day name |
{0:ddd} |
Tue |
dddd |
Full day name |
{0:dddd} |
Tuesday |
f, ff, ... |
Second fractions |
{0:fff} |
932 |
gg, ... |
Era |
{0:gg} |
A.D. |
hh |
2 digit hour |
{0:hh} |
10 |
HH |
2 digit hour, 24hr format |
{0:HH} |
22 |
mm |
Minute 00-59 |
{0:mm} |
38 |
MM |
Month 01-12 |
{0:MM} |
12 |
MMM |
Month abbreviation |
{0:MMM} |
Dec |
MMMM |
Full month name |
{0:MMMM} |
December |
ss |
Seconds 00-59 |
{0:ss} |
46 |
tt |
AM or PM |
{0:tt} |
PM |
yy |
Year, 2 digits |
{0:yy} |
02 |
yyyy |
Year |
{0:yyyy} |
2002 |
zz |
Timezone offset, 2 digits |
{0:zz} |
-05 |
zzz |
Full timezone offset |
{0:zzz} |
-05:00 |
: |
Separator |
{0:hh:mm:ss} |
10:43:20 |
/ |
Separator |
{0:dd/MM/yyyy} |
10/12/2002 |
Enumerations
Specifier |
Type |
g |
Default (Flag names if available, otherwise decimal) |
f |
Flags always |
d |
Integer always |
x |
Eight digit hex. |
二、Some Useful Examples
String.Format("{0:$#,##0.00;($#,##0.00);Zero}", value);
This will output "$1,240.00" if passed 1243.50. It will output the same format but in parentheses if the number is negative, and will output the string "Zero" if the number is zero.
String.Format("{0:(###) ###-####}", 18005551212);
This will output "(800) 555-1212".
變量.ToString()
字符型轉換 轉為字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16進制)
12345.ToString("p"); //生成 1,234,500.00%
C#:String.Format數字格式化輸出
int a = 12345678;
//格式為sring輸出
// Label1.Text = string.Format("asdfadsf{0}adsfasdf",a);
// Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf";
// Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",a);//asdfadsf¥1,234.00adsfasdf
// Label2.Text = "asdfadsf"+a.ToString("C")+"adsfasdf";//asdfadsf¥1,234.00adsfasdf
double b = 1234.12543;
a = 12345678;
//格式為特殊的string樣式輸出
// Label1.Text = string.Format("asdfadsf{0:C}adsfasdf",b);//asdfadsf¥1,234.13adsfasdf
// Label2.Text = "asdfadsf"+b.ToString("C")+"adsfasdf";//asdfadsf¥1,234.13adsfasdf
// Label1.Text = string.Format("{0:C3}",b);//¥1,234.125
// Label2.Text = b.ToString("C3");//¥1,234.125
// Label1.Text = string.Format("{0:d}",a);//十進制--12345678
// Label2.Text = b.ToString("d");//十進制--相同的類型,轉換報錯
// Label1.Text = string.Format("{0:e}",a);//指數--1.234568e+007
// Label2.Text = b.ToString("e");//指數--1.234125e+003
// Label1.Text = string.Format("{0:f}",a);//定點數--12345678.00
// Label2.Text = b.ToString("f");//定點數--1234.13
// Label1.Text = string.Format("{0:n}",a);//數值--12,345,678.00
// Label2.Text = b.ToString("n");//數值--1,234.13
// Label1.Text = string.Format("{0:x}",a);//十六進制--bc614e
// Label2.Text = b.ToString("x");//16--帶有小數不能轉換,出錯
// Label1.Text = string.Format("{0:g}",a);//通用為最緊湊--12345678
// Label2.Text = b.ToString("g");//通用為最緊湊--1234.12543
// Label1.Text = string.Format("{0:r}",a);//轉來轉去不損失精度--整數不允許用,報錯
// Label2.Text = b.ToString("r");//轉來轉去不損失精度--1234.12543
b = 4321.12543;
a = 1234;
//自定義模式輸出:
// 0 描述:占位符,如果可能,填充位
// Label1.Text = string.Format("{0:000000}",a);// 001234
// Label2.Text = string.Format("{0:000000}",b);// 004321
// # 描述:占位符,如果可能,填充位
// Label1.Text = string.Format("{0:#######}",a);// 1234
// Label2.Text = string.Format("{0:#######}",b);// 4321
// Label1.Text = string.Format("{0:#0####}",a);// 01234
// Label2.Text = string.Format("{0:0#0000}",b);// 004321
// . 描述:小數點
// Label1.Text = string.Format("{0:000.000}",a);//1234.000
// Label2.Text = string.Format("{0:000.000}",b);//4321.125
b = 87654321.12543;
a = 12345678;
// , 描述:數字分組,也用於增倍器
// Label1.Text = string.Format("{0:0,00}",a);// 12,345,678
// Label2.Text = string.Format("{0:0,00}",b);// 87,654,32
// Label1.Text = string.Format("{0:0,}",a);// 12346
// Label2.Text = string.Format("{0:0,}",b);// 87654
// Label1.Text = string.Format("{0:0,,}",a);// 12
// Label2.Text = string.Format("{0:0,,}",b);// 88
// Label1.Text = string.Format("{0:0,,,}",a);// 0
// Label2.Text = string.Format("{0:0,,,}",b);// 0
// % 描述:格式為百分數
// Label1.Text = string.Format("{0:0%}",a);// 1234567800%
// Label2.Text = string.Format("{0:#%}",b);// 8765432113%
// Label1.Text = string.Format("{0:0.00%}",a);// 1234567800.00%
// Label2.Text = string.Format("{0:#.00%}",b);// 8765432112.54%
// 'abc' 描述:顯示單引號內的文本
// Label1.Text = string.Format("{0:'文本'0}",a);// 文本12345678
// Label2.Text = string.Format("{0:文本 0}",b);// 文本87654321
// / 描述: 后跟1要打印字的字符,也用於轉移符/n等
// Label1.Text = string.Format("/"你好! /"");// "你好!"
// Label2.Text = string.Format("//c//books//new//we.asp");///c/books/new/we.asp
// @描述:后跟要打印字的字符,
// Label1.Text = string.Format(@"""你好!"""); // "你好!"要打印"則需要輸入兩對才可以
// Label2.Text = string.Format(@"/c/books/new/we.asp");///c/books/new/we.asp
百分數格式應該用“p”這個參數。
格式 原始數據 結果
"{0:P}" 0.40 40%
數字 {0:N2} 12.36
數字 {0:N0} 13
貨幣 {0:c2} $12.36
貨幣 {0:c4} $12.3656
貨幣 "¥{0:N2}" ¥12.36
科學計數法 {0:E3} 1.23E+001
百分數 {0:P} 12.25% P and p present the same.
日 期 {0:D} 2006年11月25日
日期 {0:d} 2006-11-25
日期 {0:f} 2006年11月25日 10:30
日期 {0:F} 2006年11月25日 10:30:00
日期 {0:s} 2006-11-26 10:30:00
時間 {0:T} 10:30:00
DateTime dt = DateTime.Now;
Label1.Text = dt.ToString();//2005-11-5 13:21:25
Label2.Text = dt.ToFileTime().ToString();//127756416859912816
Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
Label7.Text = dt.ToOADate().ToString();//38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();//13:21
Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
Label1.Text = dt.Year.ToString();//2005
Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();//Saturday
Label4.Text = dt.DayOfYear.ToString();//309
Label5.Text = dt.Hour.ToString();//13
Label6.Text = dt.Millisecond.ToString();//441
Label7.Text = dt.Minute.ToString();//30
Label8.Text = dt.Month.ToString();//11
Label9.Text = dt.Second.ToString();//28
Label10.Text = dt.Ticks.ToString();//632667942284412864
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05