Numeric Format Strings

 

 

 

The following table describes the standard numeric format specifiers and displays sample output produced by each format specifier.

 

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 byNumberFormatInfo.CurrencyDecimalDigits.

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 byNumberFormatInfo.NumberDecimalDigits.

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,5600

"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 byNumberFormatInfo.NumberDecimalDigits.

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

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

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

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

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

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

-1234.56 ("N3", 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 by NumberFormatInfo.PercentDecimalDigits.

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: Single, Double, 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

Any other single character

Unknown specifier

Result: Throws a FormatException at run time.

 

The following table describes the custom numeric format specifiers and displays sample output produced by each format specifier.

Format specifier

Name

Description

Examples

"0"

Zero placeholder

Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.

More information: The "0" Custom Specifier.

1234.5678 ("00000") -> 01235

0.45678 ("0.00", en-US) -> 0.46

0.45678 ("0.00", fr-FR) -> 0,46

"#"

Digit placeholder

Replaces the "#" symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.

More information: The "#" Custom Specifier.

1234.5678 ("#####") -> 1235

0.45678 ("#.##", en-US) -> .46

0.45678 ("#.##", fr-FR) -> ,46

"."

Decimal point

Determines the location of the decimal separator in the result string.

More information: The "." Custom Specifier.

0.45678 ("0.00", en-US) -> 0.46

0.45678 ("0.00", fr-FR) -> 0,46

","

Group separator and number scaling

Serves as both a group separator and a number scaling specifier. As a group separator, it inserts a localized group separator character between each group. As a number scaling specifier, it divides a number by 1000 for each comma specified.

More information: The "," Custom Specifier.

Group separator specifier:

2147483647 ("##,#", en-US) -> 2,147,483,647

2147483647 ("##,#", es-ES) -> 2.147.483.647

Scaling specifier:

2147483647 ("#,#,,", en-US) -> 2,147

2147483647 ("#,#,,", es-ES) -> 2.147

"%"

Percentage placeholder

Multiplies a number by 100 and inserts a localized percentage symbol in the result string.

More information: The "%" Custom Specifier.

0.3697 ("%#0.00", en-US) -> %36.97

0.3697 ("%#0.00", el-GR) -> %36,97

0.3697 ("##.0 %", en-US) -> 37.0 %

0.3697 ("##.0 %", el-GR) -> 37,0 %

"‰"

Per mille placeholder

Multiplies a number by 1000 and inserts a localized per mille symbol in the result string.

More information: The "‰" Custom Specifier.

0.03697 ("#0.00‰", en-US) -> 36.97‰

0.03697 ("#0.00‰", ru-RU) -> 36,97‰

"E0"

"E+0"

"E-0"

"e0"

"e+0"

"e-0"

Exponential notation

If followed by at least one 0 (zero), formats the result using exponential notation. The case of "E" or "e" indicates the case of the exponent symbol in the result string. The number of zeros following the "E" or "e" character determines the minimum number of digits in the exponent. A plus sign (+) indicates that a sign character always precedes the exponent. A minus sign (-) indicates that a sign character precedes only negative exponents.

More information: The "E" and "e" Custom Specifiers.

987654 ("#0.0e0") -> 98.8e4

1503.92311 ("0.0##e+00") -> 1.504e+03

1.8901385E-16 ("0.0e+00") -> 1.9e-16

\

Escape character

Causes the next character to be interpreted as a literal rather than as a custom format specifier.

More information: The "\" Escape Character.

987654 ("\###00\#") -> #987654#

'string'

"string"

Literal string delimiter

Indicates that the enclosed characters should be copied to the result string unchanged.

68 ("# ' degrees'") -> 68 degrees

68 ("#' degrees'") -> 68 degrees

;

Section separator

Defines sections with separate format strings for positive, negative, and zero numbers.

More information: The ";" Section Separator.

12.345 ("#0.0#;(#0.0#);-\0-") -> 12.35

0 ("#0.0#;(#0.0#);-\0-") -> -0-

-12.345 ("#0.0#;(#0.0#);-\0-") -> (12.35)

12.345 ("#0.0#;(#0.0#)") -> 12.35

0 ("#0.0#;(#0.0#)") -> 0.0

-12.345 ("#0.0#;(#0.0#)") -> (12.35)

Other

All other characters

The character is copied to the result string unchanged.

68 ("# °") -> 68 °

 

 


Copyright © 2024 pasUNITY, Inc.

 

Send comments on this topic.