Strings: Sample Generates
Strings: Sample Generates
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}
{0:n} 1.42
-12,400
{0:r} 1.42
{0:x4} System.FormatException
System.FormatException
cf90
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
Enumerations
Specifier
Type
g
Default (Flag names if available, otherwise decimal)
f
d
x
Flags always
Integer always
Eight digit hex.
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);