C#:string.format number format output {0:n2} {0:d2} {0:C2} and so on

Source: Internet
Author: User

int a = 12345678;

Format is sring output
Label1.Text = string. Format ("Asdfadsf{0}adsfasdf", a);
Label2.Text = "ASDFADSF" +a.tostring () + "ADSFASDF";
Label1.Text = string. Format ("Asdfadsf{0:c}adsfasdf", a);//ASDFADSF¥1,234.00ADSFASDF
Label2.Text = "ASDFADSF" +a.tostring ("C") + "ADSFASDF";//asdfadsf¥1,234.00adsfasdf

Double b = 1234.12543;
A = 12345678;
Format is a special string-style output
Label1.Text = string. Format ("Asdfadsf{0:c}adsfasdf", b);//asdfadsf¥1,234.13adsfasdf
Label2.Text = "ASDFADSF" +b.tostring ("C") + "ADSFASDF";//asdfadsf¥1,234.13adsfasdf
Label1.Text = string. Format ("{0:c3}", b);//¥1,234.125
Label2.Text = b.tostring ("C3");//¥1,234.125
Label1.Text = string. Format ("{0:d}", a);//Decimal--12345678
Label2.Text = b.tostring ("D");//decimal--same type, conversion error
Label1.Text = string. Format ("{0:e}", a);//Index--1.234568e+007
Label2.Text = b.tostring ("e");//Index--1.234125e+003

Label1.Text = string. Format ("{0:f}", a);//fixed-point number--12345678.00
Label2.Text = B.tostring ("f");//fixed-point number--1234.13
Label1.Text = string. Format ("{0:n}", a);//Numeric--12,345,678.00
Label2.Text = B.tostring ("n");//Numeric--1,234.13
Label1.Text = string. Format ("{0:x}", a);//Hex--bc614e
Label2.Text = b.tostring ("x"),//16--with decimal cannot be converted, error
Label1.Text = string. Format ("{0:g}", a);//Universal is the most compact--12345678
Label2.Text = b.tostring ("G");//Universal is the most compact--1234.12543
Label1.Text = string. Format ("{0:r}", a);//round-off without loss of precision-integer not allowed, error
Label2.Text = b.tostring ("R");//no loss of precision--1234.12543

b = 4321.12543;
A = 1234;
Custom mode output:
0 Description: Placeholder, if possible, fill bit
Label1.Text = string. Format ("{0:000000}", a);//001234
Label2.Text = string. Format ("{0:000000}", b);//004321
# Description: Placeholder, if possible, fill bit
Label1.Text = string. Format ("{0:#######}", a);//1234
Label2.Text = string. Format ("{0:#######}", b);//4321
Label1.Text = string. Format ("{0: #0 # #}", a);//01234
Label2.Text = string. Format ("{0:0#0000}", b);//004321

//   . Description: decimal point
Label1.Text = string. Format ("{0:000.000}", a);//1234.000
Label2.Text = string. Format ("{0:000.000}", b);//4321.125
b = 87654321.12543;
A = 12345678;
, Description: Number grouping, also used in multiplier
Label1.Text = string. Format ("{0:0,00}", a);//12,345,678
Label2.Text = string. Format ("{0:0,00}", b);//87,654,32
Label1.Text = string. Format ("{0:0,}", a);//12346
Label2.Text = string. Format ("{0:0,}", b);//87654
Label1.Text = string. Format ("{0:0,,}", a);//12
Label2.Text = string. Format ("{0:0,,}", b);//88
   label1.text = string. Format ("{0:0,,,}", a);// 0//   label2.text = string. Format ("{0:0,,,}", b);// 0//   %  description: Formatted as a percentage//   label1.text =  String. Format ("{0:0%}", a);// 1234567800%//   label2.text = string. Format ("{0:#%}", b);// 8765432113%//   label1.text = string. Format ("{0:0.00%}", a);// 1234567800.00%//   label2.text = string. Format ("{0:#.00%}", b);// 8765432112.54%//    ' abc '   Description: Displays text within single quotation marks//    Label1.text = string. Format ("{0: ' text ' 0}", a);//  text 12345678//   label2.text = string. Format ("{0:0}", b);//  text 87654321//   /  Description: followed by 1 characters to print the word, also for the transfer character/n etc//    Label1.text = string. Format ("/" Hello!) /"");//  "Hello!" "//   label2.text = string. Format ("//c//books//new//we.ASP ");///c/books/new/we.asp//   @ Description: followed by the character to print the word,//   label1.text =  String. Format (@ "" "Hello! ""); //  "Hello!" To print, you need to enter two pairs before you can//   label2.text = string. Format (@ "/c/books/new/we.asp"); the///c/books/new/we.asp percentage should be in the "P" parameter.   Format   RAW data   knots   fruit   "{0:p}"  0.40 40%

Number {0:N2} 12.36
Number {0:n0} 13
Currency {0:C2} $12.36
Currency {0:C4} $12.3656
Currency "¥{0:n2}" ¥12.36
Science notation {0:E3} 1.23E+001
Percent {0:p} 12.25% p and p present the same.
Date {0:d} November 25, 2006
Date {0:d} 2006-11-25
Date {0:f} November 25, 2006 10:30
Date {0:f} November 25, 2006 10:30:00
Date {0:s} 2006-11-26 10:30:00
Time {0:t} 10:30:00

DateTime dt = DateTime.Now;
Label1.Text = dt. ToString ();//2005-11-5 13:21:25
Label2.Text = dt. Tofiletime (). ToString ();//127756416859912816
Label3.text = dt. TOFILETIMEUTC (). ToString ();//127756704859912816
Label4.text = dt. ToLocalTime (). ToString ();//2005-11-5 21:21:25
Label5.text = dt. Tolongdatestring (). ToString ();//November 5, 2005
Label6.text = dt. Tolongtimestring (). ToString ();//13:21:25
Label7.text = dt. ToOADate (). ToString ();//38661.5565508218
Label8.text = dt. ToShortDateString (). ToString ();//2005-11-5
Label9.text = dt. Toshorttimestring (). ToString ();//13:21
Label10.text = dt. ToUniversalTime (). ToString ();//2005-11-5 5:21:25

Label1.Text = dt. Year.tostring ();//2005
Label2.Text = dt. Date.tostring ();//2005-11-5 0:00:00
Label3.text = dt. Dayofweek.tostring ();//saturday
Label4.text = dt. Dayofyear.tostring ();//309
Label5.text = dt. Hour.tostring ();//13
Label6.text = Dt.Millisecond.ToString ();//441
Label7.text = dt. Minute.tostring ();//30
Label8.text = dt. Month.tostring ();//11
Label9.text = dt. Second.tostring ();//28label10.text = dt. Ticks.tostring ();//632667942284412864label11.text = dt. Timeofday.tostring ();//13:30:28.4412864 label1.text = dt. ToString ();//2005-11-5 13:47:04label2.text = dt. AddYears (1). ToString ();//2006-11-5 13:47:04label3.text = dt. AddDays (1.1). ToString ();//2005-11-6 16:11:04label4.text = dt. AddHours (1.1). ToString ();//2005-11-5 14:53:04label5.text = dt. Addmilliseconds (1.1). ToString ();//2005-11-5 13:47:04label6.text = dt. AddMonths (1). ToString ();//2005-12-5 13:47:04label7.text = dt. AddSeconds (1.1). ToString ();//2005-11-5 13:47:05label8.text = dt. AddMinutes (1.1). ToString ();//2005-11-5 13:48:10label9.text = dt. Addticks (1000). ToString ();//2005-11-5 13:47:04label10.text = dt.compareto (DT). ToString ();//0label11.text = dt. ADD (?). ToString ();//question mark is a time period  label1.tExt = dt. Equals ("2005-11-6 16:11:04"). ToString ();//falselabel2.text = dt. Equals (DT). ToString ();//truelabel3.text = dt. GetHashCode (). ToString ();//1474088234label4.text = dt. GetType (). ToString ();//system.datetimelabel5.text = dt. GetTypeCode (). ToString ();//datetime label1.text = dt. Getdatetimeformats (' s ') [0]. ToString ();//2005-11-05t14:06:25label2.text = dt. Getdatetimeformats (' t ') [0]. ToString ();//14:06label3.text = dt. Getdatetimeformats (' y ') [0]. ToString ();//November 2005 LABEL4.TEXT = DT. Getdatetimeformats (' D ') [0]. ToString ();//November 5, 2005 Label5.text = dt. Getdatetimeformats (' D ') [1]. ToString ();//2005 11 05label6.text = dt. Getdatetimeformats (' D ') [2]. ToString ();//Saturday  2005 11 05label7.text = dt. Getdatetimeformats (' D ') [3]. ToString ();//Saturday  2005 November 5 label8.text = dt. Getdatetimeformats (' M ') [0]. ToString ();//November 5 LABEL9.TEXT = DT. Getdatetimeformats (' f ') [0]. ToString ();//November 5, 2005  14:06label10.text = dt. Getdatetimeformats (' g ') [0]. ToString ();//2005-11-5 14:06label11.text = dt. Getdatetimeformats (' R ') [0]. ToString ();//sat, 05 nov 2005 14:06:25 gmt label1.text = string. Format ("{0:d}", DT);//2005-11-5label2.text = string. Format ("{0:d}", dt);//November 5, 2005 label3.text = string. Format ("{0:f}", dt);//November 5, 2005  14:23label4.text = string. Format ("{0:f}", dt);//November 5, 2005  14:23:23label5.text = string. Format ("{0:g}", DT);//2005-11-5 14:23label6.text = string. Format ("{0:g}", DT);//2005-11-5 14:23:23label7.text = string. Format ("{0:m}", dt);//November 5 label8.text = string. Format ("{0:r}", DT);//sat, 05 nov 2005 14:23:23 gmtlabel9.text = string. Format ("{0:s}", DT);//2005-11-05t14:23:23label10.text   string. Format ("{0:t}", DT);//14:23label11.text = string. Format ("{0:t}", DT);//14:23:23label12.text = string. Format ("{0:u}", DT);//2005-11-05 14:23:23zlabel13.text = string. Format ("{0:u}", dt);//November 5, 2005  6:23:23label14.text = string. Format ("{0:y}", dt);//November 2005 label15.text = string. Format ("{0}", dt);//2005-11-5 14:23:23label16.text = string. Format ("{0:yyyymmddhhmmssffff}", DT);Stringstr1 =string.                Format ("{0:n1}", 56789); result:56,789.0
STRINGSTR2 =string.                Format ("{0:n2}", 56789); result:56,789.00
STRINGSTR3 =string.                Format ("{0:n3}", 56789); result:56,789.000
Stringstr8 =string.                Format ("{0:f1}", 56789); result:56789.0
STRINGSTR9 =string.                Format ("{0:f2}", 56789); result:56789.00
Stringstr11 = (56789/100.0).            ToString ("#.##"); result:567.89
Stringstr12 = (56789/100).              ToString ("#.##"); result:567

C or C
Currency
Console.Write ("{0:c}", 2.5); $2.50
Console.Write ("{0:c}",-2.5); ($2.50)

D or D
Decimal number
Console.Write ("{0:d5}", 25); 00025

E or E
Scientific type
Console.Write ("{0:e}", 250000); 2.500000E+005

F or F
Fixed point
Console.Write ("{0:f2}", 25); 25.00
Console.Write ("{0:f0}", 25); 25

G or G
Conventional
Console.Write ("{0:g}", 2.5); 2.5

N or

C#:string.format number format output {0:n2} {0:d2} {0:C2} and so on

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.