NullifyNetwork

The blog and home page of Simon Soanes
Skip to content
[ Log On ]

.NET Format Strings - From MSDN

These are all extracted from various places in MSDN for convenience.  This way I have one place to look them up, but they are all authored by Microsoft for MSDN.

For GUIDs!

Specifier

Format of Return Value

N

32 digits:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

D

32 digits separated by hyphens:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

B

32 digits separated by hyphens, enclosed in brackets:

{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

P

32 digits separated by hyphens, enclosed in parentheses:

(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)

For Enums!

 

 

 

 

Format string

Result

G or g

Displays the enumeration entry as a string value, if possible, and otherwise displays the integer value of the current instance. If the enumeration is defined with the Flags attribute set, the string values of each valid entry are concatenated together, separated by commas. If the Flags attribute is not set, an invalid value is displayed as a numeric entry. The following example illustrates the G format specifier.

Console.WriteLine(ConsoleColor.Red.ToString("G"));         // Displays Red
FileAttributes attributes = FileAttributes.Hidden |
                            FileAttributes.Archive;
Console.WriteLine(attributes.ToString("G"));   // Displays Hidden, Archive                               


F or f

Displays the enumeration entry as a string value, if possible. If the value can be completely displayed as a summation of the entries in the enumeration (even if the Flags attribute is not present), the string values of each valid entry are concatenated together, separated by commas. If the value cannot be completely determined by the enumeration entries, then the value is formatted as the integer value. The following example illustrates the F format specifier.

Console.WriteLine(ConsoleColor.Blue.ToString("F"));       // Displays Blue
FileAttributes attributes = FileAttributes.Hidden | 
                            FileAttributes.Archive;
Console.WriteLine(attributes.ToString("F"));   // Displays Hidden, Archive                               


D or d

Displays the enumeration entry as an integer value in the shortest representation possible. The following example illustrates the D format specifier.

Console.WriteLine(ConsoleColor.Cyan.ToString("D"));         // Displays 11
FileAttributes attributes = FileAttributes.Hidden |
                            FileAttributes.Archive;
Console.WriteLine(attributes.ToString("D"));                // Displays 34                               


X or x

Displays the enumeration entry as a hexadecimal value. The value is represented with leading zeros as necessary, to ensure that the value is a minimum eight digits in length. The following example illustrates the X format specifier.

Console.WriteLine(ConsoleColor.Cyan.ToString("X"));   // Displays 0000000B
FileAttributes attributes = FileAttributes.Hidden |
                            FileAttributes.Archive;
Console.WriteLine(attributes.ToString("X"));          // Displays 00000022                               


 

 

 

For dates!

Format specifier

Description

Examples

"d"

Short date pattern.

More information: The Short Date ("d") Format Specifier.

6/15/2009 1:45:30 PM -> 6/15/2009 (en-US)

6/15/2009 1:45:30 PM -> 15/06/2009 (fr-FR)

6/15/2009 1:45:30 PM -> 2009/06/15 (ja-JP)

"D"

Long date pattern.

More information: The Long Date ("D") Format Specifier.

6/15/2009 1:45:30 PM -> Monday, June 15, 2009 (en-US)

6/15/2009 1:45:30 PM -> 15 ???? 2009 ?. (ru-RU)

6/15/2009 1:45:30 PM -> Montag, 15. Juni 2009 (de-DE)

"f"

Full date/time pattern (short time).

More information: The Full Date Short Time ("f") Format Specifier.

6/15/2009 1:45:30 PM -> Monday, June 15, 2009 1:45 PM (en-US)

6/15/2009 1:45:30 PM -> den 15 juni 2009 13:45 (sv-SE)

6/15/2009 1:45:30 PM -> Δευτ?ρα, 15 Ιουν?ου 2009 1:45 μμ (el-GR)

"F"

Full date/time pattern (long time).

More information: The Full Date Long Time ("F") Format Specifier.

6/15/2009 1:45:30 PM -> Monday, June 15, 2009 1:45:30 PM (en-US)

6/15/2009 1:45:30 PM -> den 15 juni 2009 13:45:30 (sv-SE)

6/15/2009 1:45:30 PM -> Δευτ?ρα, 15 Ιουν?ου 2009 1:45:30 μμ (el-GR)

"g"

General date/time pattern (short time).

More information: The General Date Short Time ("g") Format Specifier.

6/15/2009 1:45:30 PM -> 6/15/2009 1:45 PM (en-US)

6/15/2009 1:45:30 PM -> 15/06/2009 13:45 (es-ES)

6/15/2009 1:45:30 PM -> 2009/6/15 13:45 (zh-CN)

"G"

General date/time pattern (long time).

More information: The General Date Long Time ("G") Format Specifier.

6/15/2009 1:45:30 PM -> 6/15/2009 1:45:30 PM (en-US)

6/15/2009 1:45:30 PM -> 15/06/2009 13:45:30 (es-ES)

6/15/2009 1:45:30 PM -> 2009/6/15 13:45:30 (zh-CN)

"M", "m"

Month/day pattern.

More information: The Month ("M", "m") Format Specifier.

6/15/2009 1:45:30 PM -> June 15 (en-US)

6/15/2009 1:45:30 PM -> 15. juni (da-DK)

6/15/2009 1:45:30 PM -> 15 Juni (id-ID)

"O", "o"

Round-trip date/time pattern.

More information: The Round-trip ("O", "o") Format Specifier.

6/15/2009 1:45:30 PM -> 2009-06-15T13:45:30.0900000

"R", "r"

RFC1123 pattern.

More information: The RFC1123 ("R", "r") Format Specifier.

6/15/2009 1:45:30 PM -> Mon, 15 Jun 2009 20:45:30 GMT

"s"

Sortable date/time pattern.

More information: The Sortable ("s") Format Specifier.

6/15/2009 1:45:30 PM -> 2009-06-15T13:45:30

"t"

Short time pattern.

More information: The Short Time ("t") Format Specifier.

6/15/2009 1:45:30 PM -> 1:45 PM (en-US)

6/15/2009 1:45:30 PM -> 13:45 (hr-HR)

6/15/2009 1:45:30 PM -> 01:45 ? (ar-EG)

"T"

Long time pattern.

More information: The Long Time ("T") Format Specifier.

6/15/2009 1:45:30 PM -> 1:45:30 PM (en-US)

6/15/2009 1:45:30 PM -> 13:45:30 (hr-HR)

6/15/2009 1:45:30 PM -> 01:45:30 ? (ar-EG)

"u"

Universal sortable date/time pattern.

More information: The Universal Sortable ("u") Format Specifier.

6/15/2009 1:45:30 PM -> 2009-06-15 20:45:30Z

"U"

Universal full date/time pattern.

More information: The Universal Full ("U") Format Specifier.

6/15/2009 1:45:30 PM -> Monday, June 15, 2009 8:45:30 PM (en-US)

6/15/2009 1:45:30 PM -> den 15 juni 2009 20:45:30 (sv-SE)

6/15/2009 1:45:30 PM -> Δευτ?ρα, 15 Ιουν?ου 2009 8:45:30 μμ (el-GR)

"Y", "y"

Year month pattern.

More information: The Year Month ("Y") Format Specifier.

6/15/2009 1:45:30 PM -> June, 2009 (en-US)

6/15/2009 1:45:30 PM -> juni 2009 (da-DK)

6/15/2009 1:45:30 PM -> Juni 2009 (id-ID)

For numbers!

 

Format specifier

Name

Description

Examples

"C" or "c"

Currency

Result: A currency value.

Supported by: All numeric types.

Precision specifier: Number of decimal digits.

Default precision specifier: Defined bySystem.Globalization.NumberFormatInfo.

More information: The Currency ("C") Format Specifier.

123.456 ("C", en-US) -> $123.46

123.456 ("C", fr-FR) -> 123,46 €

123.456 ("C", ja-JP) -> ¥123

-123.456 ("C3", en-US) -> ($123.456)

-123.456 ("C3", fr-FR) -> -123,456 €

-123.456 ("C3", ja-JP) -> -¥123.456

"D" or "d"

Decimal

Result: Integer digits with optional negative sign.

Supported by: Integral types only.

Precision specifier: Minimum number of digits.

Default precision specifier: Minimum number of digits required.

More information: The Decimal("D") Format Specifier.

1234 ("D") -> 1234

-1234 ("D6") -> -001234

"E" or "e"

Exponential (scientific)

Result: Exponential notation.

Supported by: All numeric types.

Precision specifier: Number of decimal digits.

Default precision specifier: 6.

More information: The Exponential ("E") Format Specifier.

1052.0329112756 ("E", en-US) -> 1.052033E+003

1052.0329112756 ("e", fr-FR) -> 1,052033e+003

-1052.0329112756 ("e2", en-US) -> -1.05e+003

-1052.0329112756 ("E2", fr_FR) -> -1,05E+003

"F" or "f"

Fixed-point

Result: Integral and decimal digits with optional negative sign.

Supported by: All numeric types.

Precision specifier: Number of decimal digits.

Default precision specifier: Defined bySystem.Globalization.NumberFormatInfo.

More information: The Fixed-Point ("F") Format Specifier.

1234.567 ("F", en-US) -> 1234.57

1234.567 ("F", de-DE) -> 1234,57

1234 ("F1", en-US) -> 1234.0

1234 ("F1", de-DE) -> 1234,0

-1234.56 ("F4", en-US) -> -1234.5600

-1234.56 ("F4", de-DE) -> -1234,5000

"G" or "g"

General

Result: The most compact of either fixed-point or scientific notation.

Supported by: All numeric types.

Precision specifier: Number of significant digits.

Default precision specifier: Depends on numeric type.

More information: The General ("G") Format Specifier.

-123.456 ("G", en-US) -> -123.456

123.456 ("G", sv-SE) -> -123,456

123.4546 ("G4", en-US) -> 123.5

123.4546 ("G4", sv-SE) -> 123,5

-1.234567890e-25 ("G", en-US) -> -1.23456789E-25

-1.234567890e-25 ("G", sv-SE) -> -1,23456789E-25

"N" or "n"

Number

Result: Integral and decimal digits, group separators, and a decimal separator with optional negative sign.

Supported by: All numeric types.

Precision specifier: Desired number of decimal places.

Default precision specifier: Defined bySystem.Globalization.NumberFormatInfo.

More information: The Numeric ("N") Format Specifier.

1234.567 ("N", en-US) -> 1,234.57

1234.567 ("N", ru-RU) -> 1 234,57

1234 ("N", en-US) -> 1,234.0

1234 ("N", ru-RU) -> 1 234,0

-1234.56 ("N", en-US) -> -1,234.560

-1234.56 ("N", ru-RU) -> -1 234,560

P or p

Percent

Result: Number multiplied by 100 and displayed with a percent symbol.

Supported by: All numeric types.

Precision specifier: Desired number of decimal places.

Default precision specifier: Defined bySystem.Globalization.NumberFormatInfo.

More information: The Percent ("P") Format Specifier.

1 ("P", en-US) -> 100.00 %

1 ("P", fr-FR) -> 100,00 %

-0.39678 ("P1", en-US) -> -39.7 %

-0.39678 ("P1", fr-FR) -> -39,7 %

R or r

Round-trip

Result: A string that can round-trip to an identical number.

Supported by: SingleDouble, and BigInteger.

Precision specifier: Ignored.

More information: The Round-trip ("R") Format Specifier.

123456789.12345678 ("R") -> 123456789.12345678

-1234567890.12345678 ("R") -> -1234567890.1234567

X or x

Hexadecimal

Result: A hexadecimal string.

Supported by: Integral types only.

Precision specifier: Number of digits in the result string.

More information: The HexaDecimal ("X") Format Specifier.

255 ("X") -> FF

-1 ("x") -> ff

255 ("x4") -> 00ff

-1 ("X4") -> 00FF