Posts Numeric Format Strings
Post
Cancel

Numeric Format Strings

Standard numeric format strings are used to return string equivalents of numeric types in predefined patterns. When used with ToString, a standard numeric format string parameter takes the form.

object.ToString ( "Fn" );

where F is a single alphabetic character called the format specifier, and n can be one or more digits called the precision specifier.

The following table describes the standard numeric format strings. Note that the patterns produced by these format specifiers are influenced by the settings in the Regional Options control panel. Computers using different cultures or different currency settings will display different patterns.

</col></col></col>
Format specifier Name Description
C or c Currency The number is converted to a string that represents a currency amount. The conversion is controlled by the currency format information of the NumberFormatInfo object used to format the number. The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default currency precision given by the NumberFormatInfo is used.
D or d Decimal This format is supported for integral types only. The number is converted to a string of decimal digits ( 0-9 ), prefixed by a minus sign if the number is negative. The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.
E or e Scientific ( exponential ) The number is converted to a string of the form “-d.ddd�E+ddd” or “-d.ddd�e+ddd”, where each ‘d’ indicates a digit ( 0-9 ). The string starts with a minus sign if the number is negative. One digit always precedes the decimal point. The precision specifier indicates the desired number of digits after the decimal point. If the precision specifier is omitted, a default of six digits after the decimal point is used. The case of the format specifier indicates whether to prefix the exponent with an ‘E’ or an ‘e’. The exponent always consists of a plus or minus sign and a minimum of three digits. The exponent is padded with zeros to meet this minimum, if required.
F or f Fixed-point The number is converted to a string of the form “-ddd.ddd�” where each ‘d’ indicates a digit ( 0-9 ). The string starts with a minus sign if the number is negative. The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision given by the NumberFormatInfo is used.
G or g General The number is converted to the most compact decimal form, using fixed or scientific notation. The precision specifier determines the number of significant digits in the resulting string. If the precision specifier is omitted, the number of significant digits is determined by the type of number being converted:</p>
  • Int16 or UInt16: 5 digits
  • Int32 or UInt32: 10 digits
  • Int64 or UInt64: 19 digits
  • Single: 7 digits
  • Double: 15 digits
  • Decimal: 29 digits

Trailing zeros after the decimal point are removed, and the resulting string contains a decimal point only if required.

The resulting string uses fixed-point format if the exponent of the number ( as produced by the ‘E’ format ) is less than the number of significant digits, and greater than or equal to –4. Otherwise, the resulting string uses scientific format, and the case of the format specifier controls whether the format is prefixed with an ‘E’ or an ‘e’.</td> </tr>

N or n Number The number is converted to a string of the form “-d,ddd,ddd.ddd�”, where each ‘d’ indicates a digit ( 0-9 ). The string starts with a minus sign if the number is negative. Thousand separators are inserted between each group of three digits to the left of the decimal point. The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision given by the NumberFormatInfo is used.
P or p Percent The number is converted to a string that represents a percent as defined by the NumberFormatInfo.PercentNegativePattern property or the NumberFormatInfo.PercentPositivePattern property. If the number is negative, the string produced is defined by the PercentNegativePattern and starts with a minus sign. The converted number is multiplied by 100 in order to be presented as a percentage. The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision given by NumberFormatInfo is used.
R or r Round-trip The round-trip specifier guarantees that a numeric value converted to a string will be parsed back into the same numeric value. When a numeric value is formatted using this specifier, it is first tested using the general format, with 15 spaces of precision for a Double and 7 spaces of precision for a Single. If the value is successfully parsed back to the same numeric value, then it is formatted using the general format specifer. However, if the value is not successfully parsed back to the same numeric value, then the value is formatted using 17 digits of precision for a Double and 9 digits of precision for a Single. Although a precision specifier can be appended to the round-trip format specifier, it is ignored. Round trips are given precedence over precision when using this specifier. This format is supported by floating-point types only.
X or x Hexadecimal The number is converted to a string of hexadecimal digits. The case of the format specifier indicates whether to use uppercase or lowercase characters for the hexadecimal digits greater than 9. For example, use ‘X’ to produce ‘ABCDEF’, and ‘x’ to produce ‘abcdef’. The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. This format is supported for integral types only. </tbody> </table>

This post is licensed under CC BY 4.0 by the author.