Strings
There really isnt any formatting within a string, beyond its alignment. Alignment works
for any argument being printed in a String.Format call.
Sample
Generates
String.Format(->{1,10}<-, Hello); -> Hello<String.Format(->{1,-10}<-, Hello); ->Hello <-
Numbers
Basic number formatting specifiers:
Specifier
c
d
e
f
g
n
r
x
Type
Format
Currency
Decimal (Whole number)
Scientific
Fixed point
General
Number with commas for
thousands
Round trippable
Hexadecimal
{0:c}
{0:d}
{0:e}
{0:f}
{0:g}
Output (Passed Double
1.42)
$1.42
System.FormatException
1.420000e+000
1.42
1.42
Output (Passed Int
-12400)
-$12,400
-12400
-1.240000e+004
-12400.00
-12400
{0:n} 1.42
-12,400
{0:r} 1.42
{0:x4} System.FormatException
System.FormatException
cf90
Custom number formatting:
Specifier
0
#
.
,
,.
%
e
;
Output (Passed Double
Note
1500.42)
Zero placeholder {0:00.0000} 1500.4200
Pads with zeroes.
Digit placeholder {0:(#).##} (1500).42
{0:0.0} 1500.4
Decimal point
Thousand
{0:0,0} 1,500
Must be between two zeroes.
separator
Comma adjacent to Period scales
{0:0,.} 2
Number scaling
by 1000.
{0:0%}
Percent
150042%
Multiplies by 100, adds % sign.
Exponent
Many exponent formats
{0:00e+0} 15e+2
placeholder
available.
Group separator
see below
Type
Example
The group separator is especially useful for formatting currency values which require that
negative values be enclosed in parentheses. This currency formatting example at the
bottom of this document makes it obvious:
Dates
Note that date formatting is especially dependant on the systems regional settings; the
example strings here are from my local locale.
Specifier
Type
d
Short date
D
Long date
t
Short time
T
Long time
f
Full date & time
F
Full date & time (long)
g
Default date & time
G
Default date & time (long)
M
Month day pattern
r
RFC1123 date string
s
Sortable date string
u
Universal sortable, local time
U
Universal sortable, GMT
Y
Year month pattern
Example (Passed System.DateTime.Now)
10/12/2002
December 10, 2002
10:11 PM
10:11:29 PM
December 10, 2002 10:11 PM
December 10, 2002 10:11:29 PM
10/12/2002 10:11 PM
10/12/2002 10:11:29 PM
December 10
Tue, 10 Dec 2002 22:11:29 GMT
2002-12-10T22:11:29
2002-12-10 22:13:50Z
December 11, 2002 3:13:50 AM
December, 2002
The U specifier seems broken; that string certainly isnt sortable.
Custom date formatting:
Specifier
Type
Example
Example Output
{0:dd}
dd
Day
10
{0:ddd}
ddd Day name
Tue
{0:dddd}
dddd Full day name
Tuesday
{0:fff}
f, ff, Second fractions
932
{0:gg}
gg, Era
A.D.
{0:hh}
hh
2 digit hour
10
{0:HH}
HH 2 digit hour, 24hr format
22
{0:mm}
mm Minute 00-59
38
{0:MM}
MM Month 01-12
12
{0:MMM}
MMM Month abbreviation
Dec
{0:MMMM}
MMMM Full month name
December
{0:ss}
ss
Seconds 00-59
46
{0:tt}
tt
AM or PM
PM
{0:yy}
yy
Year, 2 digits
02
{0:yyyy}
yyyy Year
2002
{0:zz}
zz
Timezone offset, 2 digits
-05
{0:zzz}
zzz Full timezone offset
-05:00
{0:hh:mm:ss} 10:43:20
:
Separator
{0:dd/MM/yyyy} 10/12/2002
/
Separator
Enumerations
Specifier
Type
g
Default (Flag names if available, otherwise decimal)
f
d
x
Flags always
Integer always
Eight digit hex.
Some Useful Examples
String.Format({0:$#,##0.00;($#,##0.00);Zero}, value);
This will output $1,240.00 if passed 1243.50. It will output the same format but in
parentheses if the number is negative, and will output the string Zero if the number is
zero.
String.Format({0:(###) ###-####}, 8005551212);
This will output (800) 555-1212.
String.Format({0:dd}, DateTime.Now)