A summary of the common ToString () method in C # _c# tutorial

Source: Internet
Author: User
Tags local time month name
For ToString such as int,double:

C

Currency

2.5.ToString ("C")

¥2.50

D

Decimal number

25.ToString ("D5")

00025

E

Scientific type

25000.ToString ("E")

2.500000E+005

F

Fixed point

25.ToString ("F2")

25.00

G

Conventional

2.5.ToString ("G")

2.5

N

Digital

2500000.ToString ("N")

2,500,000.00

X

Hexadecimal

255.ToString ("X")

Ff


Format character

Should be used

Contain righteousness

Sample

C

Number Type

Currency value for special occasions

$4834.50 (USA)

£4834.50 (UK)

D

For integer types only

A general integer

4834

E

Number Type

Scientific counting method

4.834E+003

F

Number Type

Fixed number of digits after decimal point

4384.50

G

Number Type

A General number

4384.5

N

Number Type

Usually a digital format for a special occasion

4,384.50 (Uk/usa)

4 384,50 ( European continent)

P

Number Type

Percent Count method

432,000.00%

X

For integer types only

format in

1120 ( 0x should be written if you want to displaythe 0x1120)


Custom number formatting:

Specifier Type Example Output (passed Double 1500.42) Note
0 Zero placeholder {0:00.0000} 1500.4200 Pads with zeroes.
# Digit placeholder {0: (#). # #} (1500). 42
. Decimal Point {0:0.0} 1500.4
, thousand separator {0:0,0} 1,500 Must be between two zeroes.
,. Number Scaling {0:0,.} 2 Comma adjacent to Period scales by 1000.
% Percent {0:0%} 150,042% Multiplies by, adds% sign.
E Exponent placeholder {0:00e+0} 15e+2 Many exponent formats available.
; Group Separator Below

The group separator is especially useful for formatting currency values which require that negative values are enclosed in Parentheses. This currency formatting example in the bottom of this document makes it obvious:

Dates

Note This date formatting is especially dependant on the system ' s regional settings; The example strings here is are from my local locale.

Specifier Type Example (passed System.DateTime.Now)
D Short Date 10/12/2002
D Long Date December 10, 2002
T Short time 10:11 PM
T Long time 10:11:29 PM
F Full Date & Time December, 2002 10:11 PM
F Full Date & Time (Long) December, 2002 10:11:29 PM
G Default Date & Time 10/12/2002 10:11 PM
G Default Date & Time (Long) 10/12/2002 10:11:29 PM
M Month Day pattern December 10
R RFC1123 Date String Tue, Dec 2002 22:11:29 GMT
S Sortable date string 2002-12-10t22:11:29
U Universal sortable, local time 2002-12-10 22:13:50z
U Universal sortable, GMT December, 2002 3:13:50 AM
Y Year Month pattern December, 2002

The ' U ' specifier seems broken; That string certainly isn ' t sortable.

Custom Date Formatting:

Specifier Type Example Example Output
Dd Day {0:DD} 10
Ddd Day name {0:DDD} Tue
dddd Full day name {0:DDDD} Tuesday
F, FF, ... Second Fractions {0:FFF} 932
Gg... Era {0:GG} A.D.
hh 2 digit Hour {0:HH} 10
HH 2 digit hour, 24hr format {0:HH} 22
Mm Minute 00-59 {0:mm} 38
Mm Month 01-12 {0:mm} 12
MMM Month abbreviation {0:mmm} Dec
MMMM Full month name {0:mmmm} December
Ss Seconds 00-59 {0:SS} 46
Tt AM or PM {0:TT} Pm
Yy Year, 2 digits {0:YY} 02
yyyy Year {0:YYYY} 2002
Zz Timezone Offset, 2 digits {0:zz} -05
zzz Full timezone offset {0:ZZZ} -05:00
: Separator {0:HH:MM:SS} 10:43:20
/ Separator {0:DD/MM/YYYY} 10/12/2002

Enumerations

Specifier Type
G Default (Flag names if available, otherwise decimal)
F Flags always
D Integer always
X Eight digit hex.

Some useful Examples

string.format ("{0:$#,# #0;" ($#,# #0.00); Zero} ", value);

This would output "$1,240.00" if passed 1243.50. It would output the same format but in parentheses if the # is negative, and would output the string "Zero" if the numb ER is zero.
String.Format ("{0: (###) ###-####}", 18005551212);

This would output "(800) 555-1212".

Variable. ToString ()

character conversion to string
12345.ToString ("n");//Generate 12,345.00
12345.ToString ("C");//Build ¥12,345.00
12345.ToString ("E"); Generate 1.234500e+004
12345.ToString ("F4");//Generate 12345.0000
12345.ToString ("x");//Generate 3039 (16 binary)
12345. ToString ("P"); Generate 1,234,500.00%

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.