1
0
www.mikescher.com/www/statics/blog/net_format_spec.md
2018-02-03 16:50:58 +01:00

2.3 KiB

Here my (growing) collection of C# format specifier, this is not a complete list but rather a collection of specifier I needed in the past:

###Short syntax summary

"{0,5:000;0.#;0}"
{
0           // The index of the displayed object
,5          // A padding of 5
000;0.#;0   // The format string
}
000   // The format for positive numbers (min 3 places, fill missing with zeroes)
;
0.#   // The format for negative numbers (at least one digit before decimal point, maximum one after)
;
0     // The format for zeros (one digit -  a zero)

###Align right with leading spaces

"{0,6}" // min-width: 6 character

###Digits after decimal point

"{0:0.00}" // exactly 2 digits after decimal point
"{0:0.##}" // maximal 2 digits after decimal point

###Align positive & negative numbers with leading zeros

/* integer: */
"{0:0000;-000}" // min-width: 4 character

/* float: */
"{0:0000.00;-000.00}" // min-width: 4 character + 2 decimal places

###Remove minus sign

"{0:0;0}"

###Show thousand seperator

"{0:#,0}" // only for integer

###Scale numbers

"{0:#,#}"            // normal number
"{0:#,##0, kilo}"    // (number/1000) kilo
"{0:#,##0,, mill}"   // (number/1000/1000) million
"{0:#,##0,,, bill}"  // (number/1000/1000/1000) billion

**see: ** stackoverflow.com

###Right align currency

"{0,10:#,##0.00}" // Right aligned & always 2 decimal places

**see: ** stackoverflow.com

###Align strings

"{0,-10}" // Left aligned, min-width: 10 chars
"{0,10}"  // Right aligned, min-width: 10 chars

###Datetime formatting

"s" // second
"m" // minute
"t" // AM/PM
"h" // hour (1-12)
"H" // hour (0-23)
"d" // day
"M" // month
"y" // year
"z" // timezone offset

"{0:D/M/yyyy}"            //  z.B.: 28/2/2013"

###Only show positive numbers

"{0:#.#####;'';}"

###Add a prefix/suffix

"{0:(0)}" // ( number )
"{0:_0_}" // _number_

###Format Hexadecimal

"{0:X}"    // z.B.: 138D5
"{0:x}"    // z.B.: 138d5
"{0:X8}"   // z.B.: 000138D5
"0x{0:X8}" // z.B.: 0x000138D5