whitefox 發表於 2023-5-28 11:46:32

[C#] ToString() 格式化文字 - 轉載

本帖最後由 whitefox 於 2023-5-28 11:48 編輯


SpecifierTypeExampleOutput (Passed Double 1500.42)Note
C貨幣2.5.ToString("C")¥2.50

D十進位25.ToString("D5")25

E科學符號25000.ToString("E")2.50E+05

F固定點25.ToString("F2")25

G常規2.5.ToString("G")2.5

N數字2500000.ToString("N")2,500,000.00

X十六進位255.ToString("X")F


SpecifierTypeExampleOutput (Passed Double 1500.42)Note
0Zero placeholder{0:00.0000}1500.42Pads with zeroes.
#Digit placeholder{0: (#).##}(1500).42

.Decimal point{0:0.0}1500.4

,Thousand separator{0:0,0}1,500Must be between two zeroes.
,.Number scaling{0:0,.} 2Comma adjacent to Period scales by 1000.
%Percent{0:0%}150042%Multiplies by 100, adds % sign.
eExponent placeholder{0:00e+0}1.50E+03Many exponent formats available.
;Group separatorsee below



SpecifierTypeExample (Passed System.DateTime.Now)

dShort date10/12/2002


DLong date10-Dec-02


tShort time10:11 PM


TLong time10:11:29 PM


fFull date & time 2002/12/10 22:11


FFull date & time (long)2002/12/10 22:11


gDefault date & time10/12/2002 10:11 PM


GDefault date & time (long)10/12/2002 10:11:29 PM


MMonth day pattern10-Dec


rRFC1123 date stringTue, 10 Dec 2002 22:11:29 GMT


sSortable date string2002-12-10T22:11:29


uUniversal sortable, local time2002-12-10 22:13:50Z


UUniversal sortable, GMT2002/12/11 03:13


YYear month patternDecember, 2002








The 'U' specifier seems broken; that string certainly isn't sortable.








SpecifierTypeExample Example Output

ddDay{0:dd}10

dddDay name{0:ddd}Tue

ddddFull day name{0:dddd}Tuesday

f, ff, ...Second fractions{0:fff}932

gg, ...Era{0:gg}A.D.

hh2 digit hour{0:hh}10

HH2 digit hour, 24hr format{0:HH}22

mmMinute 00-59{0:mm}38

MMMonth 01-12{0:MM}12

MMMMonth abbreviation{0:MMM}Dec

MMMMFull month name{0:MMMM}December

ssSeconds 00-59{0:ss}46

ttAM or PM{0:tt}PM

yyYear, 2 digits{0:yy}2

yyyyYear{0:yyyy}2002

zzTimezone offset, 2 digits{0:zz}-5

zzzFull timezone offset{0:zzz}-05:00

:Separator{0:hh:mm:ss}10:43:20

/Separator{0:dd/MM/yyyy}10/12/2002


SpecifierType


gDefault (Flag names if available, otherwise decimal)


fFlags always



dInteger always



xEight digit hex.


------------------------------------
轉載:https://awei791129.pixnet.net/blog/post/24989287
頁: [1]
查看完整版本: [C#] ToString() 格式化文字 - 轉載