Net Standard Datetime Format Strings Page 6

ADVERTISEMENT

Section Separators and Conditional Formatting
Custom Numeric Format Strings Output Examples
Different formatting can be applied to a string based on whether the value is positive, negative,
The following table illustrates the output created by applying some custom numeric format
or zero. To produce this behavior, a custom format string can contain up to three sections
strings to specific data types and values. The output was generated using the ToString method
separated by semicolons. These sections are described in the following table.
and the English-United States (en-US) culture.
No. of Sections
Description
Format string
Data type
Value
Output
One section
The format string applies to all values.
#####
Double
123
123
Two sections
The first section applies to positive values and zeros, and the second
00000
Double
123
00123
section applies to negative values.
(###) ### - ####
Double
1234567890
(123) 456 – 7890
If the number to be formatted is negative, but becomes zero after
rounding according to the format in the second section, then the
#.##
Double
1.2
1.2
resulting zero is formatted according to the first section.
0.00
Double
1.2
1.20
Three sections
The first section applies to positive values, the second section applies to
00.00
Double
1.2
01.20
negative values, and the third section applies to zeros.
#,#
Double
1234567890
1,234,567,890
The second section can be left empty (by having nothing between the
semicolons), in which case the first section applies to all nonzero values.
#,,
Double
1234567890
1235
If the number to be formatted is nonzero, but becomes zero after
#,,,
Double
1234567890
1
rounding according to the format in the first or second section, then the
resulting zero is formatted according to the third section.
#,##0,,
Double
1234567890
1,235
#0.##%
Double
0.086
8.6%
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
0.###E+0
Double
86000
8.6E+4
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.
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)
More .NET Cheat Sheets available at

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 6