0% found this document useful (0 votes)
78 views3 pages

C# Number Formatting Techniques

The document discusses formatting numbers as strings in C#. It provides examples of using string.Format() and .ToString() methods to format numbers with decimal points, thousand separators, and different formats for positive, negative, and zero values. It also demonstrates escaping reserved characters and including text in the formatted string. A simple online tool is mentioned for testing number format strings.

Uploaded by

Joe Brody
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views3 pages

C# Number Formatting Techniques

The document discusses formatting numbers as strings in C#. It provides examples of using string.Format() and .ToString() methods to format numbers with decimal points, thousand separators, and different formats for positive, negative, and zero values. It also demonstrates escaping reserved characters and including text in the formatted string. A simple online tool is mentioned for testing number format strings.

Uploaded by

Joe Brody
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[C#] Format Numbers as String

Examples
Some examples and tips on C# number formatting using [Link]() or
.ToString() methods.

Decimal point and Thousand separator


Use "." (point) for set the position of the decimal separetor and "," (comma)
for thousand separator.

double number = 1234.56;


[Link]("{0:0.000}", number) // 1234.560
[Link]("{0:#,0.00}", number) // 1,234.56
[Link]("{0:#,0.####}", number) // 1,234.56
// Thousand separator and number scaling
[Link]("{0:#,0}", 123000000) // 123,000,000
[Link]("{0:#,0, K}", 123000000) // 123,000 K
[Link]("{0:#,0,, M}", 123000000) // 123 M

Positive, nevative and zero format


It's possible provide different format for positivie, negative and zero-value
number by separating differet format with ";" character.

1 of 3
double number = 1234.56;
double numberNeg = -1234.56;
double numberZero = 0;
[Link]("{0:0.00}", number) // 1234.56
[Link]("{0:0.00}", numberNeg) // -1234.56
[Link]("{0:0.00}", numberZero) // 0.00
[Link]("{0:0.00;(0.00);0}", number) // 1234.56
[Link]("{0:0.00;(0.00);0}", numberNeg) // (1234.56)
[Link]("{0:0.00;(0.00);0}", numberZero) // 0
[Link]("{0:0.00;'neg: '-0.00;zero}", numberNeg) // neg: -1234.56
[Link]("{0:0.00;'neg: '-0.00;zero}", numberZero) // zero

Character escape and text


Any characters not used by the formatter is reported in the result string. If
you need to enter text with reserved characters that must be inserted
between two ' (single quote).

double numberNeg = -1234.56;


// unescaped text
[Link]("{0:0.00 Number}", number) // 1234.56 Number
[Link]("{0:0.00 #Num.}", number) // 1234.56 Num
// escaped text
[Link]("{0:0.00 '#Num.'}", number) // 1234.56 #Num.

TOOL: Test you format string


A simple tool for test your format string.

[Link](" {0:0.00} ", 1234.56 ) Test

2 of 3
© 2020 - [Link]

3 of 3

You might also like