//格式为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
double b = 1234.12543; a = 12345678; The 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 system - 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); Number of fixed points - 12345678.00 // Label2.Text = b.ToString("f"); Fixed point number - 1234.13 // Label1.Text = string. Format("{0:n}",a); Value - 12,345,678.00 // Label2.Text = b.ToString("n"); Value - 1,234.13 // Label1.Text = string. Format("{0:x}",a); HEX - BC614E // Label2.Text = b.ToString("x"); 16 - With decimal number cannot be converted, error occurred // 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); Turning around without losing accuracy - integers are not allowed to be used, errors are reported // Label2.Text = b.ToString("r"); Turn around without loss of accuracy - 1234.12543
b = 4321.12543; a = 1234; Custom Mode Output: 0 Description: Placeholder, if possible, fill the bit // Label1.Text = string. Format("{0:000000}",a); // 001234 // Label2.Text = string. Format("{0:000000}",b); // 004321 # Description: Placeholder, if possible, fill the 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: Digital grouping, also used in multipliers // 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: Format 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: Show text inside 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 character to be printed, also used to transfer the character /n etc. // Label1.Text = string. Format("/"Hello! /""); "Hello!" // Label2.Text = string. Format("//c//books//new//we.asp"); c/books/new/we.asp // @描述: followed by the character to be printed, // Label1.Text = string. Format(@""""Hello! """); "Hello!" To print" you need to enter two pairs to // Label2.Text = string. Format(@"/c/books/new/we.asp"); c/books/new/we.asp The percentage format should use the "p" parameter. Format Raw Data Result "{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 Scientific Notation {0:E3} 1.23E+001 Percentage {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} 10:30, 25 November 2006 (UTC) Date {0:F} 10:30:00, 25 November 2006 (UTC) 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(); //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(); The question mark is a time period 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 Label1.Text = dt. GetDateTimeFormats('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(); November 5, 2005 Label5.Text = dt. GetDateTimeFormats('D')[1]. ToString(); //2005 11 05 Label6.Text = dt. GetDateTimeFormats('D')[2]. ToString(); Saturday 2005 11 05 Label7.Text = dt. GetDateTimeFormats('D')[3]. ToString(); Saturday November 5, 2005 Label8.Text = dt. GetDateTimeFormats('M')[0]. ToString(); November 5 Label9.Text = dt. GetDateTimeFormats('f')[0]. ToString(); 14:06, November 05, 2005 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); November 5, 2005 Label3.Text = string. Format("{0:f}",dt); 14:23, 5 November 2005 Label4.Text = string. Format("{0:F}",dt); 14:23:23, 5 Nov 2005 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); November 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); 01/05/2005 6:23:23 AM Label14.Text = string. Format("{0:Y}",dt); November 2005 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 or C currency Console.Write("{0:C}", 2.5); //$2.50 Console.Write("{0:C}", -2.5); //($2.50)
D or d Decimal numbers 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 Regular Console.Write("{0:G}", 2.5); //2.5
n or n numbers Console.Write("{0:N}", 2500000); //2,500,000.00
X or X Hex system Console.Write("{0:X}", 250); //FA Console.Write("{0:X}", 0xffff); //FFFF |