Console.WriteLine()函數(shù)中{}輸出格式詳解(C#)
?Console.WriteLine()函數(shù)的格式一直沒(méi)怎么注意。今天同事問(wèn)起Console.WriteLine({0:D3},a)的意義,忽然發(fā)現(xiàn)不知道D代表什么意義。以前以為{0,4}是指第一個(gè)變量輸出時(shí)占8位,今天查了一下,發(fā)現(xiàn)也并不完全正確。
其中格式項(xiàng)都采用如下形式:
{index[,alignment][:formatString]}???
?
其中"index"指索引占位符,這個(gè)肯定都知道;
",alignment"按字面意思顯然是對(duì)齊方式,以","為標(biāo)記;
":formatString"就是對(duì)輸出格式的限定,以":"為標(biāo)記。
?
alignment:可選,是一個(gè)帶符號(hào)的整數(shù),指示首選的格式化字段寬度。如果“對(duì)齊”值小于格式化字符串的長(zhǎng)度,“對(duì)齊”會(huì)被忽略,并且使用格式化字符串的長(zhǎng)度作為字段寬度。如果“對(duì)齊”為正數(shù),字段的格式化數(shù)據(jù)為右對(duì)齊;如果“對(duì)齊”為負(fù)數(shù),字段的格式化數(shù)據(jù)為左對(duì)齊。如果需要填充,則使用空白。如果指定“對(duì)齊”,就需要使用逗號(hào)。
?
formatString:由標(biāo)準(zhǔn)或自定義格式說(shuō)明符組成.
?
?下表是從網(wǎng)上得來(lái):
| 字符 | 說(shuō)明 | 示例 | 輸出 |
| C | ???????? 貨幣 | string.Format("{0:C3}", 2) | $2.000 |
| D | ?????? 十進(jìn)制 | string.Format("{0:D3}", 2) | 002 |
| E | ??? 科學(xué)計(jì)數(shù)法 | 1.20E+001 | 1.20E+001 |
| G | ??????? 常規(guī) | string.Format("{0:G}", 2) | 2 |
| N | ?用分號(hào)隔開(kāi)的數(shù)字 | string.Format("{0:N}", 250000) | 250,000.00 |
| X | ????? 十六進(jìn)制 | string.Format("{0:X000}", 12) | C |
| ? | ? | string.Format("{0:000.000}", 12.2) | 012.200 |
| Specifier | Type | Format | Output? (Passed? Double 1.42) | Output? (Passed? Int -12400) |
| c | Currency | {0:c} | $1.42 | -$12,400 |
| d | Decimal (Whole number) | {0:d} | System. FormatException | -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. FormatException |
| x | Hexadecimal | {0:x4} | System. FormatException | cf90 |
?
| 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 |
?
| 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 |
?
?
CSDN中例子:
?
[c-sharp]?view plain
?copy string?myFName?=?"Fred";??string?myLName?=?"Opals";??int?myInt?=?100;??string?FormatFName?=?String.Format("First?Name?=?|{0,10}|",?myFName);??string?FormatLName?=?String.Format("Last?Name?=?|{0,10}|",?myLName);??string?FormatPrice?=?String.Format("Price?=?|{0,10:C}|",?myInt);???Console.WriteLine(FormatFName);??Console.WriteLine(FormatLName);??Console.WriteLine(FormatPrice);????FormatFName?=?String.Format("First?Name?=?|{0,-10}|",?myFName);??FormatLName?=?String.Format("Last?Name?=?|{0,-10}|",?myLName);FormatPrice?=?String.Format("Price?=?|{0,-10:C}|",?myInt);Console.WriteLine(FormatFName);??Console.WriteLine(FormatLName);??Console.WriteLine(FormatPrice);???????String 用法::
這種寫法用過(guò)沒(méi):string.Format("{0,-10}", 8)
www.cnblogs.com?2011-08-03 18:46 做 .net 開(kāi)發(fā)也若干年了,如此寫法(下面代碼中黃色高亮部分)確是我第一次見(jiàn)(更別提用了):
| 1 2 3 4 | var s1 = string.Format("{0,-10}", 8); var s2 = string.Format("{0,10}", 8); var s3 = string.Format("{0,20:yyyy-MM-dd}", DateTime.Today); var s4 = string.Format("4G 內(nèi)存便宜了{(lán)0,12:C2},我打算買{1,4}條", 145, 2) |
大括號(hào)中,索引后 分號(hào)前,有一個(gè)逗號(hào)和一個(gè)整數(shù)(減號(hào)表示負(fù)數(shù))。 會(huì)格式化成為什么樣子呢?看下調(diào)試截圖吧: 從上圖中能大致看出此端倪,沒(méi)錯(cuò): tring.Format("{0,-10}", 8) 等同于 string.Format("{0}", 8).PadRight(10); tring.Format("{0,10}", 8) 等同于 string.Format("{0}", 8).PadLeft(10)。 String.Format 方法 format 參數(shù)由零或多個(gè)文本序列與零或多個(gè)索引占位符混合組成,其中索引占位符稱為格式項(xiàng),對(duì)應(yīng)于與此方法的參數(shù)列表中的對(duì)象。 格式設(shè)置過(guò)程將每個(gè)格式項(xiàng)替換為相應(yīng)對(duì)象值的字符串表示形式。 格式項(xiàng)的語(yǔ)法如下: {index[,length][:formatString]} 方括號(hào)中的元素是可選的。 下表描述每個(gè)元素。 有關(guān)復(fù)合格式設(shè)置功能(包括格式項(xiàng)的語(yǔ)法)的更多信息,請(qǐng)參見(jiàn)復(fù)合格式。
元素說(shuō)明
| 索引 | 要設(shè)置格式的對(duì)象的參數(shù)列表中的位置(從零開(kāi)始)。 如果由 index 指定的對(duì)象為 null,則格式項(xiàng)將被 String.Empty 替換。 由于該重載在其參數(shù)列表中只有單個(gè)對(duì)象,index 的值必須始終為 0。 如果 index 位置沒(méi)有參數(shù),將引發(fā) FormatException。 |
| ,length | 參數(shù)的字符串表示形式中包含的最小字符數(shù)。 如果該值是正的,則參數(shù)右對(duì)齊;如果該值是負(fù)的,則參數(shù)左對(duì)齊。 |
| :formatString | 要設(shè)置格式的對(duì)象支持的標(biāo)準(zhǔn)或自定義格式字符串。 formatString 的可能值與該對(duì)象的 ToString(format) 方法支持的值相同。 如果沒(méi)有指定 formatString,并且要設(shè)置格式的對(duì)象實(shí)現(xiàn)了 IFormattable 接口,則將傳遞 null 作為用作 IFormattable.ToString 格式字符串的 format 參數(shù)的值。 |
元素 說(shuō)明 索引 ,length 參數(shù)的字符串表示形式中包含的最小字符數(shù)。 如果該值是正的,則參數(shù)右對(duì)齊;如果該值是負(fù)的,則參數(shù)左對(duì)齊。 :formatString MSDN 原文鏈接:Format 方法 (String, Object) ,length 其實(shí)把 String.PadLeft 和 String.PadRight 的功能融合在的 String.Format 方法中,簡(jiǎn)化了代碼的編寫。 如果沒(méi)有這種格式,代碼寫起來(lái)麻煩多了:
| 1 2 3 | var s5 = string.Format("4G 內(nèi)存便宜了{(lán)0},我打算買{1}條", 145.ToString("C2").PadLeft(12), 2.ToString().PadLeft(4)) |
看起來(lái)不直觀,復(fù)雜,如文首代碼中的 “我打算買{1,4}條”。 實(shí)際使用少,沒(méi)用過(guò)的看了很納悶。
??前言
如果你熟悉Microsoft Foundation Classes(MFC)的CString,Windows Template Library(WTL)的CString或者Standard Template Library(STL)的字符串類,那么你對(duì)String.Format方法肯定很熟悉。在C#中也經(jīng)常使用這個(gè)方法來(lái)格式化字符串,比如下面這樣: int?x?=?16; decimal?y?=?3.57m; string?h?=?String.Format(?"item?{0}?sells?at?{1:C}",?x,?y?); Console.WriteLine(h) 在我的機(jī)器上,可以得到下面的輸出: item?16?sells?at?¥3.57 也許你的機(jī)器上的輸出和這個(gè)不太一樣。這是正常的,本文稍后就會(huì)解釋這個(gè)問(wèn)題。 在我們?nèi)粘J褂弥?#xff0c;更多的是使用Console.WriteLine方法來(lái)輸出一個(gè)字符串。其實(shí)String.Format和Console.WriteLine有很多共同點(diǎn)。兩個(gè)方法都有很多重載的格式并且采用無(wú)固定參數(shù)的對(duì)象數(shù)組作為最后一個(gè)參數(shù)。下面的兩個(gè)語(yǔ)句會(huì)產(chǎn)生同樣的輸出。 Console.WriteLine(?"Hello?{0}?{1}?{2}?{3}?{4}?{5}?{6}?{7}?{8}",?123,?45.67,?true,?'Q',?4,?5,?6,?7,?'8'); string?u?=?String.Format("Hello?{0}?{1}?{2}?{3}?{4}?{5}?{6}?{7}?{8}",?123,?45.67,?true,?'Q',?4,?5,?6,?7,?'8'); Console.WriteLine(u) 輸出如下: Hello?123?45.67?True?Q?4?5?6?7?8 Hello?123?45.67?True?Q?4?5?6?7?8 2 字符串格式 String.Format和WriteLine都遵守同樣的格式化規(guī)則。格式化的格式如下:"{ N [, M ][: formatString ]}", arg1, ... argN,在這個(gè)格式中: 1) N是從0開(kāi)始的整數(shù),表示要格式化的參數(shù)的個(gè)數(shù) 2) M是一個(gè)可選的整數(shù),表示格式化后的參數(shù)所占的寬度,如果M是負(fù)數(shù),那么格式化后的值就是左對(duì)齊的,如果M是正數(shù),那么格式化后的值是右對(duì)齊的 3) formatString是另外一個(gè)可選的參數(shù),表示格式代碼 argN表示要格式化的表達(dá)式,和N是對(duì)應(yīng)的。 如果argN是空值,那么就用一個(gè)空字符串來(lái)代替。如果沒(méi)有formatString,那么就用參數(shù)N對(duì)應(yīng)的ToString方法來(lái)格式化。下面的語(yǔ)句會(huì)產(chǎn)生同樣的輸出: ublic?class?TestConsoleApp { ????public?static?void?Main(string[]?args) ????{ ????????Console.WriteLine(123); ????????Console.WriteLine("{0}",?123); ????????Console.WriteLine("{0:D3}",?123); ????} } 輸出是: 123 123 123 也可以通過(guò)String.Format得到同樣的輸出。 tring?s?=?string.Format("123"); string?t?=?string.Format("{0}",?123); string?u?=?string.Format("{0:D3}",?123); Console.WriteLine(s); Console.WriteLine(t); Console.WriteLine(u) 因此有如下結(jié)論: (,M)決定了格式化字符串的寬度和對(duì)齊方向 (:formatString)決定了如何格式化數(shù)據(jù),比如用貨幣符號(hào),科學(xué)計(jì)數(shù)法或者16進(jìn)制。就像下面這樣: Console.WriteLine("{0,5}?{1,5}",?123,?456);??????//?右對(duì)齊 Console.WriteLine("{0,-5}?{1,-5}",?123,?456);????//?左對(duì)齊 輸出是 123???456 123???456 也可以合并這些表達(dá)式,先放一個(gè)逗號(hào),再放一個(gè)冒號(hào)。就像這樣: Console.WriteLine("{0,-10:D6}?{1,-10:D6}",?123,?456) 輸出是: 000123 000456 我們可以用這種格式化特性來(lái)對(duì)齊我們的輸出。 Console.WriteLine("\n{0,-10}{1,-3}",?"Name","Salary"); Console.WriteLine("----------------"); Console.WriteLine("{0,-10}{1,6}",?"Bill",?123456); Console.WriteLine("{0,-10}{1,6}",?"Polly",?7890) 輸出是: Name??????Salary ---------------- Bill??????123456 Polly???????7890 3 格式化標(biāo)識(shí)符 標(biāo)準(zhǔn)的數(shù)學(xué)格式字符串用于返回通常使用的字符串。它們通常象X0這樣的格式。X是格式化標(biāo)識(shí)符,0是精度標(biāo)識(shí)符。格式標(biāo)識(shí)符號(hào)共有9種,它們代表了大多數(shù)常用的數(shù)字格式。就像下表所示:
| 字母 | 含義 |
| C或c | Currency 貨幣格式 |
| D或d | Decimal 十進(jìn)制格式(十進(jìn)制整數(shù),不要和.Net的Decimal數(shù)據(jù)類型混淆了) |
| E或e | Exponent 指數(shù)格式 |
| F或f | Fixed point 固定精度格式 |
| G或g | General 常用格式 |
| N或 | 用逗號(hào)分割千位的數(shù)字,比如1234將會(huì)被變成1,234 |
| P或 | Percentage 百分符號(hào)格式 |
| R或r | Round-trip? 圓整(只用于浮點(diǎn)數(shù))保證一個(gè)數(shù)字被轉(zhuǎn)化成字符串以后可以再被轉(zhuǎn)回成同樣的數(shù)字 |
| X或x | Hex 16進(jìn)制格式 |
如果我們使用下面的表達(dá)方式,讓我們看看會(huì)發(fā)生什么 ublic?class?FormatSpecApp { ????public?static?void?Main(string[]?args) ????{ ????????int?i?=?123456; ????????Console.WriteLine("{0:C}",?i);?//?¥123,456.00 ????????Console.WriteLine("{0:D}",?i);?//?123456 ????????Console.WriteLine("{0:E}",?i);?//?1.234560E+005 ????????Console.WriteLine("{0:F}",?i);?//?123456.00 ????????Console.WriteLine("{0:G}",?i);?//?123456 ????????Console.WriteLine("{0:N}",?i);?//?123,456.00 ????????Console.WriteLine("{0:P}",?i);?//?12,345,600.00?% ????????Console.WriteLine("{0:X}",?i);?//?1E240 ????} } 精度控制標(biāo)識(shí)控制了有效數(shù)字的個(gè)數(shù)或者十進(jìn)制數(shù)小數(shù)的位數(shù)。 R(圓整)格式僅僅對(duì)浮點(diǎn)數(shù)有效。這個(gè)值首先會(huì)用通用格式來(lái)格式化。對(duì)于雙精度數(shù)有15位精度,對(duì)于單精度數(shù)有7位精度。如果這個(gè)值可以被正確地解析回原始的數(shù)字,就會(huì)用通用格式符來(lái)格式化。如果不能解析回去的話,那么就會(huì)用17位精度來(lái)格式化雙精度數(shù),用9位精度來(lái)格式化單精度數(shù)。盡管我們可以在圓整標(biāo)識(shí)符后面添加有效數(shù)字的位數(shù),但是它會(huì)被忽略掉。 double?d?=?1.2345678901234567890; Console.WriteLine("Floating-Point:\t{0:F16}",?d);??//?1.2345678901234600 Console.WriteLine("Roundtrip:\t{0:R16}",?d);???????//?1.2345678901234567 如果標(biāo)準(zhǔn)格式化標(biāo)識(shí)符還不能滿足你。你可以使用圖形化格式字符串來(lái)創(chuàng)建定制的字符串輸出。圖形化格式化使用占位符來(lái)表示最小位數(shù), 最大位數(shù),定位符號(hào),負(fù)號(hào)的外觀以及其它數(shù)字符號(hào)的外觀。就像下表所示
| 符號(hào) | 名稱 | 含義 |
| 0 | 0占位符 | 用0填充不足的位數(shù) |
| # | 數(shù)字占位符 | 用#代替實(shí)際的位數(shù) |
| . | 十進(jìn)制小數(shù)點(diǎn) | ? |
| , | 千位分隔符 | 用逗號(hào)進(jìn)行千位分割,比如把1000分割成1,000 |
| % | 百分符號(hào) | 顯示一個(gè)百分標(biāo)識(shí) |
| E+0 E-0 e+0 e-0 | 指數(shù)符號(hào) | 用指數(shù)符號(hào)格式化輸出 |
| \ | 專一字符 | 用于傳統(tǒng)格式的格式化序列,比如"\n"(新行) |
| 'ABC' "ABC" | 常量字符串 | 顯示單引號(hào)或者雙引號(hào)里面的字符串 |
| ? | 區(qū)域分隔符 | 如果數(shù)字會(huì)被格式化成整數(shù),負(fù)數(shù),或者0,用;來(lái)進(jìn)行分隔 |
| ,. | 縮放符號(hào) | 數(shù)字除以1000 |
看下面的例子: double?i?=?123456.42; ????????????Console.WriteLine(); ????????????Console.WriteLine("{0:000000.00}",?i);?//123456.42 ????????????Console.WriteLine("{0:00.00000000e+0}",?i);?//12.34564200e+4 ????????????Console.WriteLine("{0:0,.}",?i);??????????//123 ????????????Console.WriteLine("{0:#0.000}",?i);?????????????//?123456.420 ????????????Console.WriteLine("{0:#0.000;(#0.000)}",?i);????????//?123456.420 ????????????Console.WriteLine("{0:#0.000;(#0.000);<zero>}",?i);?//?123456.420 ????????????Console.WriteLine("{0:#%}",?i);?????//?12345642% ????????????i?=?-123456.42; ????????????Console.WriteLine(); ????????????Console.WriteLine("{0:000000.00}",?i);?//-123456.42 ????????????Console.WriteLine("{0:00.00000000e+0}",?i);?//-12.34564200e+4 ????????????Console.WriteLine("{0:0,.}",?i);??????????//-123 ????????????Console.WriteLine("{0:#0.000}",?i);?????????????//?-123456.420 ????????????Console.WriteLine("{0:#0.000;(#0.000)}",?i);????????//?(123456.420) ????????????Console.WriteLine("{0:#0;(#0);<zero>}",?i);?//?(123456) ????????????Console.WriteLine("{0:#%}",?i);?????????????//?-12345642% ????????????i?=?0; ????????????Console.WriteLine(); ????????????Console.WriteLine("{0:0,.}",?i);??????????//0 ????????????Console.WriteLine("{0:#0}",?i);?????????????//?0 ????????????Console.WriteLine("{0:#0;(#0)}",?i);????????//?0 ????????????Console.WriteLine("{0:#0;(#0);<zero>}",?i);?//?<zero> ????????????Console.WriteLine("{0:#%}",?i);?????????????//?% 4 數(shù)字字符串的解析 所有的基礎(chǔ)類型都有ToString方法,它是從object類型中繼承過(guò)來(lái)的。所有的數(shù)值類型都有Parse方法,它用字符串為參數(shù),并且返回相等的數(shù)值。比如 ublic?class?NumParsingApp { ????public?static?void?Main(string[]?args) ????{ ????????int?i?=?int.Parse("12345"); ????????Console.WriteLine("i?=?{0}",?i); ????????int?j?=?Int32.Parse("12345"); ????????Console.WriteLine("j?=?{0}",?j); ????????double?d?=?Double.Parse("1.2345E+6"); ????????Console.WriteLine("d?=?{0:F}",?d); ????????string?s?=?i.ToString(); ????????Console.WriteLine("s?=?{0}",?s); ????} } 輸出如下 i?=?12345 j?=?12345 d?=?1234500.00 s?=?12345 在缺省狀況下,某些非數(shù)字字符是可以存在的。比如開(kāi)頭和結(jié)尾的空白。逗號(hào)和小數(shù)點(diǎn),加號(hào)和減號(hào),因此,下面的Parse語(yǔ)句是一樣的 tring?t?=?"??-1,234,567.890??"; //double?g?=?double.Parse(t);????????//?和下面的代碼干同樣的事情 double?g?=?double.Parse(t,? ????NumberStyles.AllowLeadingSign?|? ????NumberStyles.AllowDecimalPoint?| ????NumberStyles.AllowThousands?| ????NumberStyles.AllowLeadingWhite?|? ????NumberStyles.AllowTrailingWhite); Console.WriteLine("g?=?{0:F}",?g) 輸出都是這樣 g?=?-1234567.89 注意到,如果你要使用NumberStyles,就要添加對(duì)System.Globalization的引用,然后就可以使用不同NumberStyles的組合或者其中的任意一種。如果你想兼容貨幣符號(hào),就需要使用重載的Parse方法,它們采用了NumberFormatInfo對(duì)象作為一個(gè)參數(shù),然后你可以設(shè)置NumberFormatInfo的CurrencySymbol屬性來(lái)調(diào)用Parse方法,比如: 上面的代碼有如下輸出 h?=?-1234567.89 除了NumberFormatInfo,還可以使用CultureInfo類。CultureInfo代表了某種特定的文化,包括文化的名字,書寫的方式,日歷的格式。對(duì)于某種特定文化的操作是非常普遍的情況,比如格式化日期和排序。文化的命名方式遵從RFC1766標(biāo)準(zhǔn),使用<語(yǔ)言代碼2>-<國(guó)家/地區(qū)碼2>的方式,其中的<語(yǔ)言代碼2>是兩個(gè)小寫的字母,它們來(lái)自ISO639-1;<國(guó)家/地區(qū)碼2>是兩個(gè)大寫字母,它們來(lái)自ISO3166。比如,美國(guó)英語(yǔ)是“en-US"。英國(guó)英語(yǔ)是"en-GB"。特立尼達(dá)和多巴哥英語(yǔ)是"en-TT"。例如,我們可以創(chuàng)建一個(gè)美國(guó)英語(yǔ)的CultureInfo對(duì)象并且基于這種文化將數(shù)字轉(zhuǎn)換成字符串。 int?k?=?12345; CultureInfo?us?=?new?CultureInfo("en-US"); string?v?=?k.ToString("c",?us); Console.WriteLine(v) 輸出是: $12,345.00 要注意到,我們使用了重載的ToString方法,它把第一個(gè)格式化字符串當(dāng)成第一個(gè)參數(shù),將一個(gè)CultureInfo對(duì)象(執(zhí)行了IFormatProvider對(duì)象)作為第二個(gè)參數(shù)。這兒有第二個(gè)例子,對(duì)于丹麥人來(lái)說(shuō): CultureInfo?dk?=?new?CultureInfo("da-DK"); string?w?=?k.ToString("c",?dk); Console.WriteLine(w) 輸出是: kr?12.345,00 5 字符串和日期 一個(gè)日期對(duì)象有個(gè)叫Ticks的屬性。它存儲(chǔ)了自從公元1年的1月1號(hào)上午12點(diǎn)開(kāi)始的,以100納秒為間隔的時(shí)間。比如,Ticks值等于31241376000000000L表示公元100年,星期五,1月1號(hào),上午12點(diǎn)這一時(shí)間。Ticks總是以100納秒為間隔遞增。 DateTime的值以存儲(chǔ)在DateTimeFormatInfo實(shí)例里面的標(biāo)準(zhǔn)或者自定義的方式來(lái)表示。為了修改一個(gè)日期顯示的方式,DateTimeFormatInfo實(shí)例必須要是可寫的,以便我們寫入自定義的格式并且存入屬性中 using?System.Globalization; public?class?DatesApp { ????public?static?void?Main(string[]?args) ????{ ????????DateTime?dt?=?DateTime.Now; ????????Console.WriteLine(dt); ????????Console.WriteLine("date?=?{0},?time?=?{1}\n", ????????????dt.Date,?dt.TimeOfDay); ????} } 代碼會(huì)產(chǎn)生下面的輸出 23/06/2001?17:55:10 date?=?23/06/2001?00:00:00,?time?=?17:55:10.3839296 下表列出了標(biāo)準(zhǔn)的格式字符串以及相關(guān)的DateTimeFormatInfo屬性
| D | ? | ? |
| D | MM/dd/yyyy | ShortDatePattern(短日期模式) |
| D | dddd,MMMM dd,yyyy | LongDatePattern(長(zhǎng)日期模式) |
| F | dddd,MMMM dd,yyyy HH:mm | Full date and time (long date and short time)(全日期和時(shí)間模式) |
| F | dddd,MMMM dd,yyyy HH:mm: | FullDateTimePattern (long date and long time)(長(zhǎng)日期和長(zhǎng)時(shí)間) |
| G | MM/dd/yyyy HH:mm | General (short date and short time)(通用模式,短日期和短時(shí)間) |
| G | MM/dd/yyyy HH:mm: | General (short date and long time)(通用模式,短日期和長(zhǎng)時(shí)間) |
| M,M | MMMM dd | MonthDayPattern(月天模式) |
| r,R | ddd,dd MMM yyyy,HH':'mm':'ss 'GMT' | RFC1123Pattern (RFC1123模式) |
| S | yyyy-MM-dd HH:mm: | SortableDateTimePattern (conforms to ISO 8601) using local time(使用本地時(shí)間的可排序模式) |
| T | HH:mm | ShortTimePattern (短時(shí)間模式) |
| T | HH:mm: | LongTimePattern(長(zhǎng)時(shí)間模式) |
| U | yyyy-MM-dd HH:mm: | UniversalSortable-DateTimePattern (conforms to ISO 8601) using universal time(通用可排序模式) |
| U | dddd,MMMM dd,yyyy,HH:mm: | UniversalSortable-DateTimePattern(通用可排序模式) |
| y,Y | MMMM,yyyy | YearMonthPattern(年月模式) |
DateTimeFormatInfo.InvariantInfo屬性得到了默認(rèn)的只讀的DateTimeFormatInfo實(shí)例,它與文化無(wú)關(guān)。你可以創(chuàng)建自定義的模式。要注意到的是InvariantInfo不一定和本地的格式一樣。Invariant等于美國(guó)格式。另外,如果你向DateTime.Format方法傳遞的第二個(gè)參數(shù)是null,DateTimeFormatInfo將會(huì)是默認(rèn)的CurrentInfo。比如 Console.WriteLine(dt.ToString("d",?dtfi)); Console.WriteLine(dt.ToString("d",?null)); Console.WriteLine() 輸出是 06/23/2001 23/06/2001 對(duì)比選擇InvariantInfo和CurrentInfo的。 DateTimeFormatInfo?dtfi; Console.Write("[I]nvariant?or?[C]urrent?Info?:?"); if?(Console.Read()?==?'I') ????dtfi?=?DateTimeFormatInfo.InvariantInfo; else ????dtfi?=?DateTimeFormatInfo.CurrentInfo; DateTimeFormatInfo?dtfi?=?DateTimeFormatInfo.InvariantInfo; Console.WriteLine(dt.ToString("D",?dtfi)); Console.WriteLine(dt.ToString("f",?dtfi)); Console.WriteLine(dt.ToString("F",?dtfi)); Console.WriteLine(dt.ToString("g",?dtfi)); Console.WriteLine(dt.ToString("G",?dtfi)); Console.WriteLine(dt.ToString("m",?dtfi)); Console.WriteLine(dt.ToString("r",?dtfi)); Console.WriteLine(dt.ToString("s",?dtfi)); Console.WriteLine(dt.ToString("t",?dtfi)); Console.WriteLine(dt.ToString("T",?dtfi)); Console.WriteLine(dt.ToString("u",?dtfi)); Console.WriteLine(dt.ToString("U",?dtfi)); Console.WriteLine(dt.ToString("d",?dtfi)); Console.WriteLine(dt.ToString("y",?dtfi)); Console.WriteLine(dt.ToString("dd-MMM-yy",?dtfi)) 輸出是 [I]nvariant?or?[C]urrent?Info?:?I 01/03/2002 03/01/2002 Thursday,?03?January?2002 Thursday,?03?January?2002?12:55 Thursday,?03?January?2002?12:55:03 01/03/2002?12:55 01/03/2002?12:55:03 January?03 Thu,?03?Jan?2002?12:55:03?GMT 2002-01-03T12:55:03 12:55 12:55:03 2002-01-03?12:55:03Z Thursday,?03?January?2002?12:55:03 01/03/2002 2002?January 03-Jan-02 [I]nvariant?or?[C]urrent?Info?:?C 03/01/2002 03/01/2002 03?January?2002 03?January?2002?12:55 03?January?2002?12:55:47 03/01/2002?12:55 03/01/2002?12:55:47 03?January Thu,?03?Jan?2002?12:55:47?GMT 2002-01-03T12:55:47 12:55 12:55:47 2002-01-03?12:55:47Z 03?January?2002?12:55:47 03/01/2002 January?2002 03-Jan-02 /****************************************************************************************** *【Author】:flyingbread *【Date】:2007年1月18日 *【Notice】: *1、本文為原創(chuàng)技術(shù)文章,首發(fā)博客園個(gè)人站點(diǎn)( http://flyingbread.cnblogs.com/
?
轉(zhuǎn)載于:https://www.cnblogs.com/JianGuoWan/p/7684936.html
總結(jié)
以上是生活随笔為你收集整理的函数中{}输出格式详解(C#)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。