NumberFormat
Number formats: value.ToString("F2") and $"{value:F2}" (the compiler calls the functions of this
namespace).
| Format | Meaning | Example | Result |
|---|---|---|---|
Dn |
integers: at least n digits, filled with zeros | 42.ToString("D5") |
00042 |
Xn |
integers: hexadecimal (x: lower case), at least n digits |
255.ToString("X4") |
00FF |
Bn |
integers: binary, at least n digits | 5.ToString("B8") |
00000101 |
Fn |
fixed point with n decimals (default 2) | 3.14159.ToString("F2") |
3.14 |
Nn |
like F with , between groups of thousands |
1234567.ToString("N0") |
1,234,567 |
En |
scientific, n decimals (default 6; e: lower case) |
1234.5.ToString("E2") |
1.23E+003 |
Pn |
percent: times 100, n decimals (default 2) | 0.256.ToString("P1") |
25.6 % |
G |
the same text as ToString() |
The text does not depend on the platform or a locale (. and ,). Floating point values are formatted from their
exact binary value and rounded half away from zero, like .NET: 0.125.ToString("F2") is "0.13",
2.5.ToString("F0") is "3". A result that is zero has no minus sign. Negative values in X and B are written
in two’s complement of the type’s size ((-1).ToString("X") of an int32 is "FFFFFFFF").