【www.gdgbn.com--安卓教程】

c#格式化数值结果表
字符
 说明
 示例
 输出
 
c 货币 string.format("{0:c3}", 2) $2.000
d 十进制 string.format("{0:d3}", 2) 002
e 科学计数法 1.20e+001 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

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 <-


numbers
basic number formatting specifiers:

 

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


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%


protected void button1_click(object sender, eventargs e)
{
    string s1,s2,s3,s4,s5,s6,s7,s8,s9,s0;
    double num = 2.5;
    s1 = string.format("cc {0:c}", num);      //cc ¥2.50
    s2 = string.format("dd {0:d}", (int)num); //dd 2
    s3 = string.format("e  {0:e}", num);      //e  2.500000e+000
    s4 = string.format("e  {0:e}", num);      //e  2.500000e+000
    s5 = string.format("ff {0:f}", num);      //ff 2.50
    s6 = string.format("gg {0:g}", num);      //gg 2.5
    s7 = string.format("nn {0:n}", num);      //nn 2.50
    s8 = string.format("pp {0:p}", num);      //pp 250.00%
    s9 = string.format("rr {0:r}", num);      //rr 2.5
    s0 = string.format("xx {0:x}", (int)num); //xx 2

    string br = "n";
    textbox1.text = string.concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);
}

--------------------------------------------------------------------------------

标准数字格式的精度:
--------------------------------------------------------------------------------
 
protected void button1_click(object sender, eventargs e)
{
    string s1,s2,s3,s4,s5,s6,s7,s8,s9,s0;
    double num = 2.5;
    s1 = string.format("cc {0:c3}", num);      //cc ¥2.500
    s2 = string.format("dd {0:d3}", (int)num); //dd 002
    s3 = string.format("e  {0:e3}", num);      //e  2.500e+000
    s4 = string.format("e  {0:e3}", num);      //e  2.500e+000
    s5 = string.format("ff {0:f3}", num);      //ff 2.500
    s6 = string.format("gg {0:g3}", num);      //gg 2.5
    s7 = string.format("nn {0:n3}", num);      //nn 2.500
    s8 = string.format("pp {0:p3}", num);      //pp 250.000%
    s9 = string.format("rr {0:r3}", num);      //rr 2.5
    s0 = string.format("xx {0:x3}", (int)num); //xx 002

    string br = "n";
    textbox1.text = string.concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);
}

--------------------------------------------------------------------------------

自定义的数字格式:
--------------------------------------------------------------------------------
 
protected void button1_click(object sender, eventargs e)
{
    string s1,s2,s3,s4,s5,s6,s7,s8,s9,s0;
    double num = -1234.567;
    s1 = string.format("{0:000000.00}", num);         //-001234.57
    s2 = string.format("{0:######.##}", num);         //-1234.57
    s3 = string.format("{0:#,#.####}", num);          //-1,234.567
    s4 = string.format("{0:0,0.0000}", num);          //-1,234.5670
    s5 = string.format("{0:######.000000}", num);     //-1234.567000
    s6 = string.format("{0:000000.######}", num);     //-001234.567
    s7 = string.format("{0:#.00%}", num);             //-123456.70%
    s8 = string.format("{0:%#.00}", num);             //-%123456.70
    s9 = string.format("{0:(+|0)#.#;(-)#.#}", num);   //(-)1234.6
    s0 = string.format("{0:(+)#.#;(-)#.#;(0)}", num); //(-)1234.6

    string br = "n";
    textbox1.text = string.concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);
}

本文来源:http://www.gdgbn.com/shoujikaifa/28623/