asp.net string與string.formt數字

來源:互聯網
上載者:User

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);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.