Geeks With Blogs
Confessions of an Evangelist

One of the things that it seems like I am often looking up is how to use String.Format to display values in my applications. I’ve found that while Visual Studio in some situations such as WPF XAML editing has features built in, in other cases it is lacking. For that reason I wanted to post a table with some of the more common expressions.  I found this useful, hopefully you will too.

Basic numeric formatting 

Format specifier

Name

1234.56 (double)

-100000 (int)

Description

{0:c}

Currency

$123.45

($-100,000.00)

The number is converted to a string that represents a currency amount.

{0:d}

Decimal

(error)

-100000

Display a decimal amount, but throws an exception on non-decimal values (floats).

{0:e}

Scientific

1.234500e+002

-1.000000e+005

The number is converted to a string of the form “-d.ddd…E+ddd” or “-d.ddd…e+ddd”, where each ‘d’ indicates a digit (0-9).

{0:f}

Fixed-point

123.45

-100000.00

The number is converted to a string of the form “-ddd.ddd…” where each ‘d’ indicates a digit (0-9). The string starts with a minus sign if the number is negative.

{0:g}

General

123.45

-100000

The default format

{0:n}

Number

123.45

-100,000.00

Thousand separators are inserted between each group of three digits to the left of the decimal point.

{0:p}

Percent

12,345.00%

-10,000,000.00 %

The number is converted to a percentage.

{0:r}

Round-Trip

123.45

(error)

Guarantees that the number can be returned to its original value on conversion from string to number.

{0:x} or {0:X}

Hexadecimal

(error)

FFFE7960

Use ‘X’ to produce “ABCDEF”, and ‘x’ to produce “abcdef”.

Custom formatting

Name

Format

Example

Input

Output

Zero placeholder

0

{0:000.00}

45.5

045.50

Digit placeholder

#

{0:###.##}

45.5

45.5

Decimal point

.

{0:00.00}

45.5

45.50

Thousand separator (requires 0)

,

{0:0,0}

1000000

1,000,000

Percent (multiplies by 100)

%

{0:0%}

45.5

4550%

Scientific notation (multiple formats)

E0,E+0,E-0,e0,e+0,e-0

{0:E0}

1000000

1E+006

Date formats 

Specifier

Description

Example

{0:d}

Short date

2/4/2009

{0:D}

Long date

Wednesday, February 04, 2009

{0:t}

Short time

2:59 PM

{0:T}

Long time

2:59:14 PM

{0:f}

Full date/time (short time)

Wednesday, February 04, 2009 2:59 PM

{0:F}

Full date/time (long time)

Wednesday, February 04, 2009 2:59:40 PM

{0:g}

General date/time (short time)

2/4/2009 2:59 PM

{0:G}

General date/time (long time)

2/4/2009 3:01:12 PM

{0:M}

Month day

February 4

{0:R}

RFC1123

Wed, 04 Feb 2009 15:01:55 GMT

{0:s}

Sortable date/time ; conforms to ISO 8601

2009-02-04T15:02:10

{0:u}

Universal sortable date/time

2009-02-04 15:02:28Z

{0:U}

Universal sortable date/time

Wednesday, February 04, 2009 7:03:11 AM

{0:Y}

Year month

February, 2009


Custom Date formatting
 

Specifier

Description

Example

Output

d

Day of the month as a number

{0:d}

4

dd

Day of the month as a number, with leading zero

{0:dd}

04

ddd

Abbriviated day of the month name

{0:ddd}

Wed

dddd

Full day of the month name

{0:dddd}

Wednesday

f,ff,fff

Fraction of a second (repeat “f” for more precision)

{0:fff}

405

gg,ggg

The era

{0:gg}

A.D.

h

Hour (1-12 range)

{0:h}

3

hh

Hour (1-12 range, with leading zero for 0-9)

{0:hh}

03

H

Hour (0-23 range)

{0:H}

15

HH

Hour (0-23 range with leading zero for 0-9)

{0:HH}

03

m

Minutes (0-59)

{0:m}

9

mm

Minutes (0-59 range with leading zero for 0-9)

{0:mm}

09

M

Number of the month (0-12)

{0:M}

2

MM

Number of the month (0-12 with leading zero for 0-9)

{0:MM}

02

MMM

Abbreviated name of the month

{0:MMM}

Feb

MMMM

Specifies the full name of the month

{0:MMMM}

February

s

Seconds (0-59 range)

{0:s}

23

ss

Seconds (0-59 range with leading zero for 0-9)

{0:ss}

23

tt

Both characters of the AM/PM range

{0:tt}

PM

y

The first two digits of the year (0-99)

{0:y}

9

yy

The first two digits of the year (0-99) with leading zero for 0-9

{0:yy}

09

yyyy

Four digit year number, if the year is less than 1000 it has 0′s prepended

{0:yyyy}

 

zz

The timezone offset in whole hours

{0:zz}

+08

zzz

The timezone offset in hours and minutes

{0:zzz}

+08:00

:

Time seperator

{0:hh:mm}

03:33

/

Date seperator

{0:MM/yyyy}

02/2009

Posted on Tuesday, October 8, 2013 3:45 PM | Back to top

Copyright © Mike Benkovich | Powered by: GeeksWithBlogs.net