Documente Academic
Documente Profesional
Documente Cultură
Technologies
Downloads
Sign in
Programs
Community
Documentation
Was this page helpful? Your feedback about this content is important. Let us know what you think.
Yes
MSDN subscriptions
Get tools
Samples
No
Collapse All
Export 0
MSDN Library
Development Tools and Languages
Visual Studio 2008
Important
This document may not represent best practices for current development, links to downloads and other resources may no
longer be valid. Current recommended version can be found here.
Visual Studio
.NET Framework Programming in
Visual Studio
.NET Framework Core Development
Development Fundamentals
Other Versions
Base Types
Formatting Types
Numeric Format Strings
Standard Numeric Format
Strings
Custom Numeric Format
Strings
Format
specifier
"0"
Name
Description
Examples
Zero
placeholder
Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in
the result string.
1234.5678
"00000" >
01235
0.45678
"0.00", en
US > 0.46
0.45678
"0.00", frFR
> 0,46
"#"
Digit
placeholder
Replaces the pound sign with the corresponding digit if one is present; otherwise, no digit
appears in the result string.
More information: The "#" Custom Specifier.
1234.5678
"#####" >
1235
0.45678
"#.##", en
US > .46
0.45678
"#.##", frFR
> ,46
"."
Decimal
point
0.45678
"0.00", en
US > 0.46
0.45678
"0.00", frFR
> 0,46
","
Group
separator
and
number
scaling
Serves as both a group separator and a number scaling specifier. As a group separator, it
inserts a localized group separator character between each group. As a number scaling
specifier, it divides a number by 1000 for each comma specified.
Group
separator
specifier:
2147483647
"##,#", en
US >
2,147,483,647
2147483647
"##,#", es
ES >
2.147.483.647
Scaling
specifier:
2147483647
"#,#,,", en
US > 2,147
2147483647
"#,#,,", esES
> 2.147
"%"
Percentage
placeholder
Multiplies a number by 100 and inserts a localized percentage symbol in the result string.
More information: The "%" Custom Specifier.
0.3697
"%#0.00",
enUS >
%36.97
0.3697
"%#0.00", el
GR >
%36,97
0.3697 "##.0
%", enUS >
37.0 %
0.3697 "##.0
%", elGR >
37,0 %
""
Per mille
placeholder
Multiplies a number by 1000 and inserts a localized per mille symbol in the result string.
More information: The "" Custom Specifier.
0.03697
"#0.00",
enUS >
36.97
0.03697
"#0.00",
ruRU >
36,97
"E0"
"E0"
If followed by at least one 0 zero, formats the result using exponential notation. The case
of "E" or "e" indicates the case of the exponent symbol in the result string. The number of
zeros following the "E" or "e" character determines the minimum number of digits in the
exponent. A plus sign + indicates that a sign character always precedes the exponent. A
minus sign indicates that a sign character precedes only negative exponents.
"e0"
"E+0"
Exponential
notation
"e+0"
Escape
character
Causes the next character to be interpreted as a literal rather than as a custom format
specifier.
More information: The "\" Escape Character.
'string'
"string"
1503.92311
"0.0##e+00"
> 1.504e+03
1.8901385E
16
"0.0e+00"
> 1.9e16
"e0"
987654
"#0.0e0" >
98.8e4
Literal
string
delimiter
Indicates that the enclosed characters should be copied to the result string unchanged.
987654
"\###00\#"
> #987654#
68 "# '
degrees'" >
68 degrees
68 "#'
degrees'" >
68 degrees
Section
separator
Defines sections with separate format strings for positive, negative, and zero numbers.
More information: The ";" Section Separator.
12.345
"#0.0#;
#0.0#;\0"
> 12.35
0 "#0.0#;
#0.0#;\0"
> 0
12.345
"#0.0#;
#0.0#;\0"
> 12.35
12.345
"#0.0#;
#0.0#" >
12.35
0 "#0.0#;
#0.0#" >
0.0
12.345
"#0.0#;
#0.0#" >
12.35
Other
All other
characters
The following sections provide detailed information about each of the custom numeric format specifiers.
VB
doublevalue;
value=123;
Console.WriteLine(value.ToString("00000"));
//Displays00123
value=1.2;
Console.WriteLine(value.ToString("0.00",CultureInfo.InvariantCulture));
//Displays1.20
Console.WriteLine(value.ToString("00.00",CultureInfo.InvariantCulture));
//Displays01.20
Console.WriteLine(value.ToString("00.00",
CultureInfo.CreateSpecificCulture("daDK")));
//Displays01,20
value=.56;
Console.WriteLine(value.ToString("0.0",CultureInfo.InvariantCulture));
//Displays0.6
value=1234567890;
Console.WriteLine(value.ToString("0,0",CultureInfo.InvariantCulture));
//Displays1,234,567,890
Console.WriteLine(value.ToString("0,0",
CultureInfo.CreateSpecificCulture("elGR")));
//Displays1.234.567.890
value=1234567890.123456;
Console.WriteLine(value.ToString("0,0.0",CultureInfo.InvariantCulture));
//Displays1,234,567,890.1
value=1234.567890;
Console.WriteLine(value.ToString("0,0.00",CultureInfo.InvariantCulture));
//Displays1,234.57
Back to table
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.
The following example displays several values that are formatted by using custom format strings that include digit placeholders.
C#
VB
doublevalue;
value=1.2;
Console.WriteLine(value.ToString("#.##",CultureInfo.InvariantCulture));
//Displays1.2
value=123;
Console.WriteLine(value.ToString("#####"));
//Displays123
value=123456;
Console.WriteLine(value.ToString("[######]"));
//Displays[123456]
value=1234567890;
Console.WriteLine(value.ToString("#"));
//Displays1234567890
Console.WriteLine(value.ToString("(###)#######"));
//Displays(123)4567890
Back to table
VB
doublevalue;
value=1.2;
Console.WriteLine(value.ToString("0.00",CultureInfo.InvariantCulture));
//Displays1.20
Console.WriteLine(value.ToString("00.00",CultureInfo.InvariantCulture));
//Displays01.20
Console.WriteLine(value.ToString("00.00",
CultureInfo.CreateSpecificCulture("daDK")));
//Displays01,20
value=.086;
Console.WriteLine(value.ToString("#0.##%",CultureInfo.InvariantCulture));
//Displays8.6%
value=86000;
Console.WriteLine(value.ToString("0.###E+0",CultureInfo.InvariantCulture));
//Displays8.6E+4
Back to table
You can use group separator and number scaling specifiers in the same format string. For example, if the string "#,0,," and the invariant
culture are used to format the number one billion, the output is "1,000".
The following example illustrates the use of the comma as a group separator.
C#
VB
doublevalue=1234567890;
Console.WriteLine(value.ToString("#,#",CultureInfo.InvariantCulture));
//Displays1,234,567,890
Console.WriteLine(value.ToString("#,##0,,",CultureInfo.InvariantCulture));
//Displays1,235
The following example illustrates the use of the comma as a specifier for number scaling.
C#
VB
doublevalue=1234567890;
Console.WriteLine(value.ToString("#,,",CultureInfo.InvariantCulture));
//Displays1235
Console.WriteLine(value.ToString("#,,,",CultureInfo.InvariantCulture));
//Displays1
Console.WriteLine(value.ToString("#,##0,,",CultureInfo.InvariantCulture));
//Displays1,235
Back to table
VB
doublevalue=.086;
Console.WriteLine(value.ToString("#0.##%",CultureInfo.InvariantCulture));
//Displays8.6%
Back to table
VB
doublevalue=.00354;
stringperMilleFmt="#0.##"+'\u2030';
Console.WriteLine(value.ToString(perMilleFmt,CultureInfo.InvariantCulture));
//Displays3.54
Back to table
VB
doublevalue=86000;
Console.WriteLine(value.ToString("0.###E+0",CultureInfo.InvariantCulture));
//Displays8.6E+4
Console.WriteLine(value.ToString("0.###E+000",CultureInfo.InvariantCulture));
//Displays8.6E+004
Console.WriteLine(value.ToString("0.###E000",CultureInfo.InvariantCulture));
//Displays8.6E004
Back to table
Note:
Some compilers, such as the C++ and C# compilers, may also interpret a single backslash character as an escape character. To ensure
that a string is interpreted correctly when formatting, you can use the verbatim string literal character the @ character before the
string in C#, or add another backslash character before each backslash in C# and C++. The following C# example illustrates both
approaches.
The following example uses the escape character to prevent the formatting operation from interpreting the "#", "0", and "\" characters
as either escape characters or format specifiers. The C# examples uses an additional backslash to ensure that a backslash is interpreted
as a literal character.
C#
VB
intvalue=123;
Console.WriteLine(value.ToString("\\#\\#\\###0dollarsand\\0\\0cents\\#\\#\\#"));
//Displays###123dollarsand00cents###
Console.WriteLine(value.ToString(@"\#\#\###0dollarsand\0\0cents\#\#\#"));
//Displays###123dollarsand00cents###
Console.WriteLine(value.ToString("\\\\\\\\\\\\##0dollarsand\\0\\0cents\\\\\\\\\\\\"));
//Displays\\\123dollarsand00cents\\\
Console.WriteLine(value.ToString(@"\\\\\\##0dollarsand\0\0cents\\\\\\"));
//Displays\\\123dollarsand00cents\\\
Back to table
Number of
sections
Description
One
section
Two
sections
The first section applies to positive values and zeros, and the second section applies to negative values.
Three
sections
The first section applies to positive values, the second section applies to negative values, and the third section applies
to zeros.
If the number to be formatted is negative, but becomes zero after rounding according to the format in the second
section, the resulting zero is formatted according to the first section.
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, 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.
The following example uses the ";" format specifier to format positive, negative, and zero numbers differently.
C#
VB
doubleposValue=1234;
doublenegValue=1234;
stringfmt="##;(##)";
Console.WriteLine(posValue.ToString(fmt));//Displays1234
Console.WriteLine(negValue.ToString(fmt));//Displays(1234)
Back to table
Notes
Floating-Point Infinities and NaN
Regardless of the format string, if the value of a Single or Double floatingpoint type is positive infinity, negative infinity, or not a
number NaN, the formatted string is the value of the respective PositiveInfinitySymbol, NegativeInfinitySymbol, or NaNSymbol
property specified by the currently applicable NumberFormatInfo object.
Example
The following example demonstrates two custom numeric format strings. In both cases, the digit placeholder # displays the numeric
data, and all other characters are copied to the result string.
C#
VB
doublenumber1=1234567890;
stringvalue1=number1.ToString("(###)#######");
Console.WriteLine(value1);
intnumber2=42;
stringvalue2=number2.ToString("MyNumber=#");
Console.WriteLine(value2);
//Theexampledisplaysthefollowingoutput:
//(123)4567890
//MyNumber=42
See Also
Tasks
How to: Pad a Number with Leading Zeros
Concepts
Formatting Overview
Standard Numeric Format Strings
Reference
NumberFormatInfo
Change History
Date
History
Reason
October 2009
Customerfeedback.
July2009
Revised extensively.
Informationenhancement.
July2008
Contentbugfix.
Community Additions
ADD
Yes
Make a suggestion
No
Dev centers
Windows
Office
Learning resources
Community
Support
Forums
Self support
Channel 9
Blogs
Interoperability Bridges
Codeplex
MSDN Magazine
Visual Studio
Microsoft Azure
More...
Programs
BizSpark for startups
DreamSpark
Imagine Cup
Newsletter
Terms of use
Trademarks
2015 Microsoft