Custom Numeric Format Strings Output Examples | |||
---|---|---|---|
The following table illustrates the output created by applying some custom numeric format strings to specific data types and values. The output was generated using the ToString method and the English-United States (en-US) culture. | |||
Format string | Data type | Value | Output |
##### | Double | 123 | 123 |
00000 | Double | 123 | 00123 |
(###) ### - #### | Double | 1234567890 | (123) 456 – 7890 |
#.## | Double | 1.2 | 1.2 |
0.00 | Double | 1.2 | 1.20 |
00.00 | Double | 1.2 | 01.20 |
#,# | Double | 1234567890 | 1,234,567,890 |
#,, | Double | 1234567890 | 1235 |
#,,, | Double | 1234567890 | 1 |
#,##0,, | Double | 1234567890 | 1,235 |
#0.##% | Double | 0.086 | 8.6% |
0.###E+0 | Double | 86000 | 8.6E+4 |
0.###E+000 | Double | 86000 | 8.6E+004 |
0.###E-000 | Double | 86000 | 8.6E004 |
[##-##-##] | Double | 123456 | [12-34-56] |
##;(##) | Double | 1234 | 1234 |
##;(##) | Double | -1234 | (1234) |
Friday, April 2, 2010
Section Separators and Conditional Formatting
Section Separators and Conditional Formatting | |
---|---|
Different formatting can be applied to a string based on whether the value is positive, negative, or zero. To produce this behavior, a custom format string can contain up to three sections separated by semicolons. These sections are described in the following table. | |
No. of Sections | Description |
One section | The format string applies to all values. |
Two sections | The first section applies to positive values and zeros, and the second section applies to negative values. If the number to be formatted is negative, but becomes zero after rounding according to the format in the second section, then the resulting zero is formatted according to the first section. |
Three sections | The first section applies to positive values, the second section applies to negative values, and the third section applies to zeros. The second section can be left empty (by having nothing between the semicolons), in which case the first section applies to all nonzero values. If the number to be formatted is nonzero, but becomes zero after rounding according to the format in the first or second section, then the resulting zero is formatted according to the third section. |
Section separators ignore any preexisting formatting associated with a number when the final value is formatted. For example, negative values are always displayed without a minus sign when section separators are used. If you want the final formatted value to have a minus sign, you should explicitly include the minus sign as part of the custom format specifier. |
.NET Custom Number Format Strings
.NET Custom Number Format Strings | ||
---|---|---|
Specifier | Name | Description |
0 | Zero placeholder | If the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the result string. The position of the leftmost '0' before the decimal point and the rightmost '0' after the decimal point determines the range of digits that are always present in the result string. The "00" specifier causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "00" would result in the value 35. |
# | Digit placeholder | If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string. This specifier never displays the '0' character if it is not a significant digit, even if '0' is the only digit in the string. It will display the '0' character if it is a significant digit in the number being displayed. The "##" format string causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "##" would result in the value 35. |
. | Decimal point | The first '.' character in the format string determines the location of the decimal separator in the formatted value; any additional '.' characters are ignored. The actual character used as the decimal separator is determined by the NumberDecimalSeparator property of the NumberFormatInfo that controls formatting. |
, | Thousand separator and number scaling | Thousand Separator Specifier:If one or more ',' characters is specified between two digit placeholders (0 or #) that format the integral digits of a number, a group separator character is inserted between each number group in the integral part of the output. The NumberGroupSeparator and NumberGroupSizes properties of the current NumberFormatInfo object determine the character used as the number group separator and the size of each number group. Number Scaling Specifier:If one or more ',' characters is specified immediately to the left of the explicit or implicit decimal point, the number to be formatted is divided by 1000 each time a number scaling specifier occurs. For example, if the string "0,," is used to format the number 100 million, the output is "100". You can use thousand separator and number scaling specifiers in the same format string. |
% | Percentage placeholder | The presence of a '%' character in a format string causes a number to be multiplied by 100 before it is formatted. The appropriate symbol is inserted in the number itself at the location where the '%' appears in the format string. The percent character used is dependent on the current NumberFormatInfo class. |
E0 E+0 E-0 e0 e+0 e-0 | Scientific notation | If any of the strings "E", "E+", "E-", "e", "e+", or "e-" are present in the format string and are followed immediately by at least one '0' character, then the number is formatted using scientific notation with an 'E' or 'e' inserted between the number and the exponent. The number of '0' characters following the scientific notation indicator determines the minimum number of digits to output for the exponent. The "E+" and "e+" formats indicate that a sign character (plus or minus) should always precede the exponent. The "E", "E-", "e", or "e-" formats indicate that a sign character should only precede negative exponents. |
\ | Escape character | In C# and C++, the backslash character causes the next character in the format string to be interpreted as an escape sequence. It is used with traditional formatting sequences like '\n' (new line). In some languages, the escape character itself must be preceded by an escape character when used as a literal. Otherwise, the compiler interprets the character as an escape sequence. Use the string "\\" to display '\'. Note that this escape character is not supported in Visual Basic; however, ControlChars provides the same functionality. |
'ABC' "ABC" | Literal string | Characters enclosed in single or double quotes are copied to the result string, and do not affect formatting. |
; | Section separator | The ';' character is used to separate sections for positive, negative, and zero numbers in the format string. |
Other | All other characters | Any other character is copied to the result string, and does not affect formatting. |
.NET Custom DateTime Format Strings
Format String
|
Data type
|
Value
|
Output
|
---|---|---|---|
C | Double | 12345.6789 | $12,345.68 |
D | Int32 | 12345 | 12345 |
D8 | Int32 | 12345 | 00012345 |
E | Double | 12345.6789 | 1.234568E+004 |
E10 | Double | 12345.6789 | 1.2345678900E+004 |
E | Double | 12345.6789 | 1.2346e+004 |
Format String
|
Data type
|
Value
|
Output
|
---|---|---|---|
F | Double | 12345.6789 | 12345.68 |
F0 | Double | 12345.6789 | 123456 |
F6 | Double | 12345.6789 | 12345.678900 |
G | Double | 12345.6789 | 12345.6789 |
G7 | Double | 12345.6789 | 12345.68 |
G | Double | 0.0000023 | 2.3E-6 |
Format String
|
Data type
|
Value
|
Output
|
---|---|---|---|
G2 | Double | 1234 | 1.2E3 |
G | Double | Math.PI | 3.14159265358979 |
N | Double | 12345.6789 | 12,345.68 |
N4 | Double | 123456789 | 123,456,789.0000 |
P | Double | .126 | 12.60 % |
r | Double | Math.PI | 3.141592653589793 |
.NET Standard Number Format Strings
.NET Standard Number Format Strings | ||
---|---|---|
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 current NumberFormatInfo object. Precision specifier (eg. “{0:C5}” allowed. |
D or d | Decimal | This format is supported only for integral types. The number is converted to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative. Precision specifier (eg. “{0:d3}” allowed. |
E or e | Scientific (expol) | 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. Precision specifier (eg. “{0:E5}” allowed. 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. Precision specifier (eg. “{0:f4}” allowed. |
G or g | General | The number is converted to the most compact of either fixed-point or scientific notation, depending on the type of the number and whether a precision specifier is present. |
N or n | Number | The number is converted to a string of the form "-d,ddd,ddd.ddd…", where '-' indicates a negative number symbol if required, 'd' indicates a digit (0-9), ',' indicates a thousand separator between number groups, and '.' indicates a decimal point symbol. The actual negative number pattern, number group size, thousand separator, and decimal separator are specified by the current NumberFormatInfo object. Precision specifier (eg. “{0:N5}” allowed. |
P or p | Percent | The number is converted to a string that represents a percent as defined by the NumberFormatInfo.PercentNegativePattern property if the number is negative, or the NumberFormatInfo.PercentPositivePattern property if the number is positive. The converted number is multiplied by 100 in order to be presented as a percentage. Precision specifier (eg. “{0:p6}” allowed. |
R or r | Round-trip | This format is supported only for the Single and Double types. 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, it is formatted using the general format specifier. 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. Precision specifier NOT allowed. |
X or x | Hexa decimal | This format is supported only for integral types. 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. Precision specifier (eg. "{0:x4}" allowed. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier. |
Any other single char. | (Unknown specifier) | An unknown specifier throws a runtime format exception. |
Subscribe to:
Posts (Atom)