C#:string. Format number formatted output

Source: Internet
Author: User

1. Various commonly used digital formattingCurrency conversion C or C (2 digits after the default decimal point). C3 followed by the number represents the decimal after several)
String. Format ("{0:c}", a) <==>a.tostring ("C")//$2.50
String. Format ("{0:c3}", a) <==>a.tostring ("C3")//$2.500 converted to decimal number D or D a=25 string. Format ("{0:d}", a) <==>b.tostring ("D")//is converted to a decimal string. Format ("{0:d5}", a) <==>a.tostring ("D5")//00025 scientific type Count E or e a=250000 string. Format ("{0:e}", a) <==>a.tostring ("E")//2.500000e+005 fixed point F or F a=25 string. Format ("{0:f}", a) <==>a.tostring ("F")//25 string. Format ("{0:f2}", a) <==>a.tostring ("F2")//25.00 general numeric G or G a=2.5 string. Format ("{0:g}", a) <==>a.tostring ("G")//2.5 number converts n or n a=2500000 string. Format ("{0:n}", a) <==>a.tostring ("N")//2,500,000.00 is converted to a hexadecimal x or x a=250 string. Format ("{0:x}", a) <==>a.tostring ("X")//2,500,000.00//far or R string. Format ("{0:r}", a), <==>b.tostring ("R");//round-off without loss of precision-integer not allowed, error 1234.12543
2. Custom mode output:
0 Description: Placeholder, if possible, fill bit # Description: Placeholder, if possible, fill bit (with 0 of the padding is different). Description: Decimal, Description: Number grouping, also used for multiplier
String. Format ("{0:000000}", a);//001234
String. Format ("{0:000000.00}", b);//004321.13
String. Format ("{0:0#0####}", a);//0001234 (#被逼填充位)
String. Format ("{0:#####}", a);//1234 (#尽可能的不填充)
b = 87654321.12543;
A = 12345678;
, Description: Number grouping, also used in multiplier
String. Format ("{0:0,00}", a);//12,345,678
String. Format ("{0:0,00}", b);//87,654,321
String. Format ("{0:0,}", a);//12346
String. Format ("{0:0,}", b);//87654
String. Format ("{0:0,,}", a);//12
String. Format ("{0:0,,}", b);//88
String. Format ("{0:0,,,}", a);//0
String. Format ("{0:0,,,}", b);//0
% Description: Format is a percentage of string. Format ("{0:p}", a);//40.00%
String. Format ("{0:0%}", a);//1,234,567,800%
String. Format ("{0:#%}", b);//8,765,432,113%
String. Format ("{0:0.00%}", a);//1,234,567,800%
String. Format ("{0:#.00%}", b);//8,765,432,112,.54%
' ABC ' Description: Display text within single quotation marks
String. Format ("{0: ' text ' 0}", a);//Text 12345678
String. Format ("{0: Text 0}", b);//Text 87654321
\ Description: followed by 1 characters to print the word, also for the transfer character \ n, etc.
String. Format ("\" Hello!) \ "");//"Hello!" "
String. Format ("\\c\\books\\new\\we.asp");//\c\books\new\we.asp
@ Description: followed by the character to print the word,
String. Format (@ "" "Hello! """); Hello "To print" you need to enter two pairs to
String. Format (@ "\c\books\new\we.asp");//\c\books\new\we.asp
3. Date Conversion
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;
Dt. ToString ();//2005-11-5 13:21:25
Dt. Tofiletime (). ToString ();//127756416859912816
Dt. TOFILETIMEUTC (). ToString ();//127756704859912816
Dt. ToLocalTime (). ToString ();//2005-11-5 21:21:25
Dt. Tolongdatestring (). ToString ();//November 5, 2005
Dt. Tolongtimestring (). ToString ();//13:21:25
Dt. ToOADate (). ToString ();//38661.5565508218
Dt. ToShortDateString (). ToString ();//2005-11-5
Dt. Toshorttimestring (). ToString ();//13:21
Dt. ToUniversalTime (). ToString ();//2005-11-5 5:21:25
Dt. Year.tostring ();//2005
Dt. Date.tostring ();//2005-11-5 0:00:00
Dt. Dayofweek.tostring ();//saturday
Dt. Dayofyear.tostring ();//309
Dt. Hour.tostring ();//13
Dt.Millisecond.ToString ();//441
Dt. Minute.tostring ();//30
Dt. Month.tostring ();//11
Dt. Second.tostring ();//28
Dt. Ticks.tostring ();//632667942284412864
Dt. Timeofday.tostring ();//13:30:28.4412864
Dt. ToString ();//2005-11-5 13:47:04
Dt. AddYears (1). ToString ();//2006-11-5 13:47:04
Dt. AddDays (1.1). ToString ();//2005-11-6 16:11:04
Dt. AddHours (1.1). ToString ();//2005-11-5 14:53:04
Dt. Addmilliseconds (1.1). ToString ();//2005-11-5 13:47:04
Dt. AddMonths (1). ToString ();//2005-12-5 13:47:04
Dt. AddSeconds (1.1). ToString ();//2005-11-5 13:47:05
Dt. AddMinutes (1.1). ToString ();//2005-11-5 13:48:10
Dt. Addticks (1000). ToString ();//2005-11-5 13:47:04
Dt.compareto (DT). ToString ();//0
Dt. ADD (?). ToString ();//question mark is a time period
Dt. Equals ("2005-11-6 16:11:04"). ToString ();//false
Dt. Equals (DT). ToString ();//true
Dt. GetHashCode (). ToString ();//1474088234
Dt. GetType (). ToString ();//system.datetime
Dt. GetTypeCode (). ToString ();//datetime
Dt. Getdatetimeformats (' s ') [0]. ToString ();//2005-11-05t14:06:25
Dt. Getdatetimeformats (' t ') [0]. ToString ();//14:06
Dt. Getdatetimeformats (' y ') [0]. ToString ();//November 2005
Dt. Getdatetimeformats (' D ') [0]. ToString ();//November 5, 2005
Dt. Getdatetimeformats (' D ') [1]. ToString ();//2005 11 05
Dt. Getdatetimeformats (' D ') [2]. ToString ();//Saturday 2005 11 05
Dt. Getdatetimeformats (' D ') [3]. ToString ();//Saturday November 5, 2005
Dt. Getdatetimeformats (' M ') [0]. ToString ();//November 5
Dt. Getdatetimeformats (' f ') [0]. ToString ();//November 5, 2005 14:06
Dt. Getdatetimeformats (' g ') [0]. ToString ();//2005-11-5 14:06
Dt. Getdatetimeformats (' R ') [0]. ToString ();//sat, 2005 14:06:25 GMT
String. Format ("{0:d}", DT);//2005-11-5
String. Format ("{0:d}", dt);//November 5, 2005
String. Format ("{0:f}", dt);//November 5, 2005 14:23
String. Format ("{0:f}", dt);//November 5, 2005 14:23:23
String. Format ("{0:g}", DT);//2005-11-5 14:23
String. Format ("{0:g}", DT);//2005-11-5 14:23:23
String. Format ("{0:m}", dt);//November 5
String. Format ("{0:r}", DT);//sat, 2005 14:23:23 GMT
String. Format ("{0:s}", DT);//2005-11-05t14:23:23
String. Format ("{0:t}", DT);//14:23
String. Format ("{0:t}", DT);//14:23:23
String. Format ("{0:u}", DT);//2005-11-05 14:23:23z
String. Format ("{0:u}", dt);//November 5, 2005 6:23:23
String. Format ("{0:y}", dt);//November 2005
String. Format ("{0}", dt);//2005-11-5 14:23:23
String. Format ("{0:yyyymmddhhmmssffff}", DT);

C#:string. Format number formatted output

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.