Net Standard Datetime Format Strings Page 4

ADVERTISEMENT

.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
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
(exponential)
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
Hexadecimal
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
(Unknown
An unknown specifier throws a runtime format exception.
single char.
specifier)
Examples (en-US)
Format String
Data type
Value
Output
Format String
Data type
Value
Output
Format String
Data type
Value
Output
C
F
G2
Double
12345.6789
$12,345.68
Double
12345.6789
12345.68
Double
1234
1.2E3
D
F0
G
Int32
12345
12345
Double
12345.6789
123456
Double
Math.PI
3.14159265358979
D8
Int32
12345
00012345
F6
Double
12345.6789
12345.678900
N
Double
12345.6789
12,345.68
E
Double
12345.6789
1.234568E+004
G
Double
12345.6789
12345.6789
N4
Double
123456789
123,456,789.0000
E10
G7
P
Double
12345.6789
1.2345678900E+004
Double
12345.6789
12345.68
Double
.126
12.60 %
E
G
r
Double
12345.6789
1.2346e+004
Double
0.0000023
2.3E-6
Double
Math.PI
3.141592653589793
More .NET Cheat Sheets available at

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 6