欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#中的常用ToString()方法总结

程序员文章站 2023-11-05 21:43:10
对于int,double等的tostring: c     货币     2.5.tostri...
对于int,double等的tostring:

c

 

 

货币

 

 

2.5.tostring("c")

 

 

¥2.50

 

 

d

 

 

十进制数

 

 

25.tostring("d5")

 

 

00025

 

 

e

 

 

科学型

 

 

25000.tostring("e")

 

 

2.500000e+005

 

 

f

 

 

固定点

 

 

25.tostring("f2")

 

 

25.00

 

 

g

 

 

常规

 

 

2.5.tostring("g")

 

 

2.5

 

 

n

 

 

数字

 

 

2500000.tostring("n")

 

 

2,500,000.00

 

 

x

 

 

十六进制

 

 

255.tostring("x")

 

 

ff


格式符

 

 

 

c

数字类型

专用场合的货币值

$4834.50 (usa)

£4834.50 (uk)

d

只用于整数类型

一般的整数

4834

e

数字类型

科学计数法

4.834e+003

f

数字类型

小数点后的位数固定

4384.50

g

数字类型

一般的数字

4384.5

n

数字类型

通常是专用场合的数字 格式

4,384.50 (uk/usa)

4 384,50 (欧洲大陆)

p

数字类型

百分比计数法

432,000.00%

x

只用于整数类型

16进制格式

1120 (如果要显示0x1120,需要写上0x)


custom number formatting:

 

specifier type example output (passed double 1500.42) 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:

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%