Developers can make use of numeric format specifiers in Console.WriteLine() statements in C# when working with numeric data.
Typical syntax for a standard numeric format string is:
format specifier][precision specifier]
where:
format specifier: a single alphabetic character specifying type of number format, such as currency or percent.precision specifier: an optional integer that affects number of digits in the resulting string. .NET 7.0 onwards, the maximum precision value is 999,999,999. Note that the precision specifier only controls the number of digits in the string representation of a number. It does not round the number itself.
Any numeric format string that contains more than one alphabetic character, including white space, is interpreted as a custom numeric format string. For example, a numeric format specifier can be suffixed with digits specifying number of zeros to be inserted after a decimal location. If you use a specifier such as C2, two zeros will be suffixed after the decimal location of the given number.
The following table lists some of the standard and custom numeric format specifiers that you have in C#
| Format Specifier | Standard/Custom | Description |
| C or c | Standard | A standard numeric format for Currency. The number is converted to a string that represents a currency amount. |
| D or d | Standard | A standard numeric format for Decimal. The number is converted to a string of decimal digits (0-9), prefixed by a minus sign in case the number is negative. The precision specifier indicates the minimum number of digits desired in the resulting string. This format is supported for fundamental types only. |
| E or e | Standard | A standard numeric format for 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). |
| 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). If the number is negative, the string starts with a minus sign. |
| 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). If the number is negative, the string starts with a minus sign. |
| X or x | Hexadecimal | The number is converted to a string of hexadecimal digits. It uses "X" to produce "ABCDEF", and "x" to produce "abcdef. |
| G or g | Standard | A standard numeric format for General format. It converts a number to the more compact of either fixed-point or scientific notation, depending on type of the number and whether a precision specifier is present. If the precision specifier is not present or zero, the type of the number determines the default precision. |
| 0 | Custom | It serves as a zero-placeholder symbol. If the value being formatted contains a digit where ‘0’ appears, then it is copied to the result string. |
| # | Custom | It serves as a digit-placeholder symbol. If the value being formatted contains a digit where ‘#’ appears, then, it is copied to the result string. |
| . | Custom | The first ‘.’ character verifies the location of the decimal separator. |
| , | Custom | The ‘,’ character serves as a thousand separator specifier and a number scaling specifier. |
| % | Custom | The ‘%’ character in a format string multiplies a number with 100 before it is formatted. |
| ; | Custom | The ‘;’ character separates a section into positive, negative, and zero numbers |
| E0, E+0,E-0, e0, e+0, e-0 | Custom | If any of the given strings are present in the format string and they are followed by at least one ‘0’ character, then the number is formatted using scientific notation. |
| \ | Custom | The backslash character causes the next character in the format string to be interpreted as an escape sequence. |
| ‘ABC’ or "ABC" | Custom | The characters that are enclosed within single or double quotes are copied to the result string. |




Join the conversation! Your thoughts help the community grow.