character display conversion c # {0: d} string.Format ()

character display conversion c # {0: d} string.Format ()

This is the difference between the actual and the first few months of a written nothing in nature. But this is more clear, easy to learn

 

character display conversion c # {0: d}

C #: String.Format digital formatted output:

int a = 12345678;
//格式为sring输出
// 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

B = 1234.12543 Double;
A = 12345678;
// format for a particular output pattern string
// Label1.Text = string.Format ( "asdfadsf {0: C} adsfasdf", B); // asdfadsf ¥ 1,234.13adsfasdf
// Label2 = .text "asdfadsf" b.ToString + ( "C") + "adsfasdf"; ¥ 1,234.13adsfasdf asdfadsf //
// Label1.Text = string.Format ( "{0: a C3}", B); // ¥ 1,234.125
// b.ToString Label2.Text = ( "a C3"); // ¥ 1,234.125
// Label1.Text = string.Format ( "{0: D}", A); // decimal --12,345,678
// Label2 .Text = b.ToString ( "d") ; // decimal - the same type, the conversion error
// Label1.Text = string.Format ( "{0 : e}", a); // index --1.234568 007 + E
// b.ToString Label2.Text = ( "E"); // index --1.234125e + 003

// Label1.Text = string.Format ( "{0 : f}", a); // set point --12345678.00
// b.ToString Label2.Text = ( "F"); // set point --1234.13
// Label1.Text = string.Format ( "{0 : n}", a); // value --12,345,678.00
// b.ToString Label2.Text = ( "n-"); // value --1,234.13
// Label1.Text = string.Format ( "{0: x}", a); // hexadecimal --bc614e
// b.ToString Label2.Text = ( "X"); // with 16-- decimal conversion is not, an error
// Label1.Text = string.Format ( "{0 : g}", a); // common to most compact --12,345,678
// b.ToString Label2.Text = ( "G"); // general the most compact --1234.12543
@ Label1.Text = string.Format ( "{0: R & lt}", A); // spinning without loss of accuracy - not allowed to use an integer, given
// Label2 .Text = b.ToString ( "r") ; // do not turn to loss of precision --1234.12543

B = 4321.12543;
A = 1234;
// custom mode output:
@ 0 Description: placeholder, if possible, stuffing bits
Label1.Text string.Format = // ( "{0: 000000}", A); // 001 234
// string.Format Label2.Text = ( "{0: 000000}", B); // 004 321
// # description: placeholder, if possible, stuffing bits
// Label1.Text = string.Format ( "{0: #######}", A); 1234 //
// String = Label2.Text. the Format ( "{0: #######}", B); 4321 //
// Label1.Text = string.Format ( "{0: ####} # 0", A); // 01234
// string.Format Label2.Text = ( "{0: 0} # 0000", B); // 004 321

// Description: decimal.
@ Label1.Text = string.Format ( "{0: 000.000}", A); 1234.000 //
// string.Format Label2.Text = ( "{0: 000.000}", B) ; // 4321.125
B = 87,654,321.12543;
A = 12345678;
//, description: digital packet, but also for doublers
// Label1.Text = string.Format ( "{0 : 0,00}", a); / / 12,345,678
// string.Format Label2.Text = ( "{0: 0,00}", B); 87,654,32 //
// Label1.Text = string.Format ( "{0: 0,}", A ); 12346 //
// string.Format Label2.Text = ( "{0: 0,}", B); 87654 //
// Label1.Text = string.Format ( "{0: 0 ,,}", A); 12 is //
// string.Format Label2.Text = ( "{0: 0 ,,}", B); // 88
// Label1.Text = string.Format ( "{0: 0 ,,, } ", A); // 0
// string.Format Label2.Text = (" {0: 0} ,,, ", B);// 0
//% Description: format as a percentage
Label1.Text string.Format = // ( "{0: 0}%", A); 1234567800% //
// string.Format Label2.Text = ( "{0:%} #", B); // % 8,765,432,113
// Label1.Text = string.Format ( "{0: 0.00%}", A); 1,234,567,800.00% //
// string.Format Label2.Text = ( "{0: # 00%.}", B ); 8,765,432,112.54% //
// 'ABC' description: text in single quotes
// Label1.Text = string.Format ( "{0 : ' text' 0}", a); // text 12345678
// Label2 .Text = string.Format ( "{0: text 0}", b); // text 87654321
@ / description: 1 followed by the word to be printed characters, but also for transferring symbols / n, etc.
// Label1.Text = string.Format ( "/" Hello / "!"); // "Hello!"
@ string.Format Label2.Text = ( "Books // C // // // we.asp new new"); ///c/books/new/we.asp
// @ description: followed by the word you want to print characters,
// Label1.Text = string.Format (@ """Hello!", ""); //! "Hello" to be printed "is required before they can enter two pairs
// Label2.Text = string.Format(@"/c/books/new/we.asp");///c/books/new/we.asp

 

Percentage format should be used "p" this parameter.

Format of the raw data results
"{0: P}" 0.40 40%
numbers {0: N2} 12.36
numbers {0: N0} 13
Currency {0: c2} $ 12.36
Currency {0: c4} $ 12.3656
currency "¥ {0: N2} "¥ 12.36
scientific notation {0: E3} 1.23E + 001
percent {0: P} 12.25% P and p present the same.
date {0: D} 25 November 2006
date {0: d} 2006- 11-25
date {0: f}, 2006 at 10:30 on November 25
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();//2005年11月5日
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();//28
Label10.Text = dt.Ticks.ToString();//632667942284412864
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864

Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();//0
Label11.Text = dt.Add(?).ToString();//问号为一个时间段

Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
Label2.Text = dt.Equals(dt).ToString();//True
Label3.Text = dt.GetHashCode().ToString();//1474088234
Label4.Text = dt.GetType().ToString();//System.DateTime
Label5.Text = dt.GetTypeCode().ToString();//DateTime

= Dt.GetDateTimeFormats Label1.Text ( 'S') [0] .ToString (); // 2005-11-05T14: 06: 25
Label2.Text = dt.GetDateTimeFormats ( 'T') [0] .ToString () ; // 14:06
Label3.Text = dt.GetDateTimeFormats ( 'Y') [0] .ToString (); // November 2005
Label4.Text = dt.GetDateTimeFormats ( 'D') [0] .ToString ( ); // 5 November 2005
Label5.Text = dt.GetDateTimeFormats ( 'D') [. 1] .ToString (); // 200511 05
Label6.Text = dt.GetDateTimeFormats ( 'D') [2] .ToString (); // Saturday, 05 200 511
Label7.Text = dt.GetDateTimeFormats ( 'D') [3] .ToString (); // Saturday, November 5, 2005
Label8.Text = dt.GetDateTimeFormats ( 'M ') [0] .ToString () ; // 5 November
Label9.Text = dt.GetDateTimeFormats (' f ') [0] .ToString (); // 5 November 2005, 14:06
Label10.Text = dt.GetDateTimeFormats ( 'g') [ 0].ToString();//2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT

Label1.Text = string.Format("{0:d}",dt);//2005-11-5
Label2.Text = string.Format("{0:D}",dt);//2005年11月5日
Label3.Text = string.Format("{0:f}",dt);//2005年11月5日 14:23
Label4.Text = string.Format("{0:F}",dt);//2005年11月5日 14:23:23
Label5.Text = string.Format("{0:g}",dt);//2005-11-5 14:23
Label6.Text = string.Format("{0:G}",dt);//2005-11-5 14:23:23
Label7.Text = string.Format("{0:M}",dt);//11月5日
Label8.Text = string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text = string.Format("{0:s}",dt);//2005-11-05T14:23:23
Label10.Text string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23
Label16.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 或 c
货币
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
SCIENCES type
Console.Write ( "{0: E} ", 250000); //2.500000E+005

F 或 f
固定点
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 n
digital
Console.Write ( "{0: N} ", 2500000); //2,500,000.00

X or X
hexadecimal
Console.Write ( "{0: X}", 250); // the FA
Console.Write ( "{0: X}", 0xFFFF); // FFFF

https://www.cnblogs.com/zhangruisoldier/p/7852650.html

Released three original articles · won praise 2 · Views 2828

Guess you like

Origin blog.csdn.net/fangyuan621/article/details/105136203