C # String.Format Daquan

Source: Internet
Author: User

C # String.Format Daquan

Method Results Description

String. Format ("{0:d3}", 23)

023

Formatting decimal digits

String. Format ("{0:d2}", 1223)

1223

The precision specifier indicates the minimum number of digits required in the result string

12345.ToString ("n");

Generate 12,345.00

Thousands separated, two decimals
numbers separated by semicolons and specified by the number of digits after the decimal point

String. Format ("{0:n}", 14200)

14,200.00

The default is two digits after the decimal point

String. Format ("{0:n3}", 14200.2458)

14,200.246

Auto rounding

12345.ToString ("E");

Build 1.234500e+004

Scientific counting method

12345.ToString ("F4");

Build 12345.0000

Keep decimal digits

12345.ToString ("F")

?

Sentinel

12345.ToString ("X");
12345.ToString ("x4");
12345.ToString ("X2");

Build 3039 (16 binary)

HEX

Uppercase and lowercase

General "G" and "G"

? ?

12345.ToString ("G");

12345.ToString ("G");

?

Conventional

? ? ?

Custom number formats

? ?

String.Format ("{0:00.0000}", 1500.42)

1500.4200

Zero placeholder

String.Format ("{0: (#). # #}", 1500.42)

(1500). 42

Digit placeholder

String.Format ("{0:0.0}", 1500.42)

1500.4

Decimal point

String.Format ("{0:0,0}", 1500.42)

1,500

?

String.Format ("{0:0,.}", 1500.42)

2

?

String.Format ("{0:0%}", 1500.42)

150,042%

Percentage

String.Format ("{0:00e+0}", 1500.42)

15e+2

Index placeholder

? ? ?

Currency

? ?

12345.ToString ("C");

Generate ¥12,345.00

Currency symbol, thousands separated, two decimals

String. Format ("{0:c}", 0.2)

¥0.20

English operating system results: $0.20
Related to the environment of the system

String. Format ("{0:c1}", 23.15)

¥23.2

(Intercept will be rounded automatically)

String. Format ("Market price:{0:c}, preferential price {1:c}", 23.15,19.82)

?

Formatting multiple instances of object

String.Format ("{0:¥#,# #0.00; (¥#,# #0.00); 0}", 98005.30)

¥98,005.30

Amount, thousands separator, two decimals

String.Format ("{0:¥#,# #0.00; (¥#,# #0.00); 0}",-98005.30)

(¥98,005.30)

Amount, thousands separator, two decimals
Negative numbers in parentheses

String.Format ("{0:¥#,# #0.00; (¥#,# #0.00); 0}", 0)

Zero

Amount, thousands separator, two decimals
0 value in text representation

? ? ?

Percentage

? ?

12345.ToString ("P");

Generate 1,234,500.00%

Percent, thousands separated, two decimals

String. Format ("{0:p}", 0.24583)

24.58%

Two decimal places with the default retention percent

String. Format ("{0:P1}", 0.24583)

24.6%

Auto rounding

? ? ?

Roundtrip process

Double.tostring ("R") double.tostring ("R")

Single.tostring ("R") single.tostring ("R")

?

Round-trip procedure for double and single

? ? ?

Zero placeholder and digit placeholder

String. Format ("{0:0000.00}", 12394.039)

12394.04

?

String. Format ("{0:0000.00}", 194.039)

0194.04

?

String. Format ("{0:###.##}", 12394.039)

12394.04

?

String. Format ("{0:####.#}", 194.039)

194

?
? ? ?

Date Time

? ?

String. Format ("{0:d}", System.DateTime.Now)

2009-3-20

Month position is not 03

String. Format ("{0:d}", System.DateTime.Now)

March 20, 2009

?

String. Format ("{0:f}", System.DateTime.Now)

March 20, 2009 15:37

?

String. Format ("{0:f}", System.DateTime.Now)

March 20, 2009 15:37:52

?

String. Format ("{0:g}", System.DateTime.Now)

2009-3-20 15:38

?

String. Format ("{0:g}", System.DateTime.Now)

2009-3-20 15:39:27

?

String. Format ("{0:m}", System.DateTime.Now)

March 20

?

String. Format ("{0:t}", System.DateTime.Now)

15:41

?

String. Format ("{0:t}", System.DateTime.Now)

15:41:50

?

String. Format ("{0:r}", System.DateTime.Now)

Sun, June 10:12:56 GMT

RFC1123 Date format

String. Format ("{0:s}", System.DateTime.Now)

2012-06-17t10:15:12

Sort Date String

String. Format ("{0:y}", System.DateTime.Now)

June 2012

?

String. Format ("{0:u}", System.DateTime.Now)

June 17, 2012 2:19:51

General Sort, GMT time

String. Format ("{0:u}", System.DateTime.Now)

2012-06-17 10:19:51z

General sort, local time

? ? ?

Date Time (Custom)

? ?

{0:DD}

10

Day

{0:DDD}

Tue

Day name

{0:DDDD}

Tuesday

Full day name

{0:FFF}

932

Milliseconds

{0:GG}

A.D.

Era

{0:HH}

10

2 digit Hour

{0:HH}

22

2 digit hour, 24hr format

{0:mm}

38

Minute 00-59

{0:mm}

12

Month 01-12

{0:mmm}

Dec

Month abbreviation

{0:mmmm}

December

The name of the month

{0:SS}

46

Number of seconds from 00 to 59

{0:TT}

Pm

Am or PM (morning or afternoon)

{0:YY}

02

Year (two digits)

{0:YYYY}

2002

Years

{0:zz}

-05

Timezone Offset, 2 digits

{0:ZZZ}

-05:00

Full timezone offset

{0:HH:MM:SS}

10:43:20

Separator

{0:DD/MM/YYYY}

10/12/2002

Separator

? ? ?

String

? ?

String.Format ("->{0,10}<-", "Hello");

hello<-

At least 10 characters, not enough front (positive) fill empty

String.Format ("->{0,-10}<-", "Hello")

->hello <-

At least 10 characters, not enough back (negative) to fill empty

? ? ?

Phone Number Customization

? ?

String.Format ("{0: (# # #) ###-####}", 18005551212)

(1800) 555-1212

?

String.Format ("{0: (# #) ####-########}", 86075487870252)

(86) 0754-87870252

?

String.Format ("{0: (# #) ####-########}", 086075487870252)

(86) 0754-87870252

?
? ? ?

Zero placeholder "0"

? ?

int i=120;
Console.WriteLine (i.ToString ("00,000.00"));
Console.WriteLine ("{0:00,000.00}", i);

00,120.00

?
? ? ?

Digit placeholder "#"

? ?

Double d=12050.8520;
Console.WriteLine (d.tostring ("##,###.##"));
Console.WriteLine ("{0:##,###.##}", D);

12,050.85

?
? ? ?
? ? ?

C # String.Format Daquan

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.