Format a Double in C#: Decimal Places, Padding and Culture

To control how a double is printed, pass a format string to ToString. The patterns you need most are 0.00 for exactly two decimals, 0.## for at most two, and N2 for thousands separators.

All output on this page is produced with CultureInfo.InvariantCulture, so it is the same on every machine. Leave the culture out and a German or French system prints 123,46 instead of 123.46.

Decimal places

using System.Globalization;

var inv = CultureInfo.InvariantCulture;

// Exactly two decimal places
Console.WriteLine(123.4567.ToString("0.00", inv)); // 123.46
Console.WriteLine(123.45.ToString("0.00", inv));   // 123.45
Console.WriteLine(123.0.ToString("0.00", inv));    // 123.00

// At most two decimal places
Console.WriteLine(123.4567.ToString("0.##", inv)); // 123.46
Console.WriteLine(123.4.ToString("0.##", inv));    // 123.4
Console.WriteLine(123.0.ToString("0.##", inv));    // 123

// At least two digits before the decimal point
Console.WriteLine(123.4567.ToString("00.0", inv)); // 123.5
Console.WriteLine(12.3456.ToString("00.0", inv));  // 12.3
Console.WriteLine(1.2345.ToString("00.0", inv));   // 01.2
Console.WriteLine((-1.2345).ToString("00.0", inv)); // -01.2

In a custom pattern, 0 means “always print a digit here, using 0 if there is none” and # means “print a digit only if it is significant”. Digits before the decimal point are never cut off: 00.0 still prints all three digits of 123.

The standard specifiers cover the common cases with less typing:

Format1234567.891Meaning
F21234567.89Fixed-point, 2 decimals
N21,234,567.89With thousands separators, 2 decimals
N01,234,568With thousands separators, rounded to a whole number
#,##0.001,234,567.89Custom equivalent of N2
E21.23E+006Scientific notation

In interpolated strings

Put the format after a colon. A number after a comma sets the minimum width, which is handy for lining up columns:

double price = 9.5;
double total = 1234.5;

string line = string.Create(inv, $"Price: {price,8:0.00} Total: {total,10:N2}");
Console.WriteLine(line);
// Price:     9.50 Total:   1,234.50

A plain $"..." uses the current culture. string.Create(IFormatProvider, ...) (.NET 6 and later) or FormattableString.Invariant($"...") fixes it.

Different formats for positive, negative and zero

A custom pattern can have up to three sections separated by semicolons: positive, negative, zero. Accountants like this one:

const string Accounting = "#,##0.00;(#,##0.00);-";

Console.WriteLine(1500.0.ToString(Accounting, inv));  // 1,500.00
Console.WriteLine((-1500.0).ToString(Accounting, inv)); // (1,500.00)
Console.WriteLine(0.0.ToString(Accounting, inv));     // -

The negative section does not add a minus sign by itself, which is why the parentheses work.

Rounding: double is binary

0.00 rounds, it does not truncate. But it rounds the binary value the double actually holds, and most decimal fractions cannot be stored exactly. A value written as 2.675 is stored as 2.67499999999999982236431605997495353221893310546875, so depending on the format and the runtime it may come out as 2.67 or 2.68. Formatting is not the place to fix that.

If the number is money or anything else that must round like it does on paper, use decimal. 2.675m.ToString("0.00", inv) reliably returns 2.68, because decimal stores 2.675 exactly and formatting rounds a midpoint away from zero.

For the same reason, do not round with string formatting and parse the result back. Use Math.Round(amount, 2, MidpointRounding.AwayFromZero) on a decimal when you need the rounded number itself, and format only for display. Note that Math.Round without a MidpointRounding argument uses banker’s rounding (to even): Math.Round(2.5) returns 2.

Round-tripping a double

Since .NET Core 3.0, double.ToString() with no format returns the shortest string that parses back to the identical value, so you no longer need the old "R" format:

double sum = 0.1 + 0.2;
Console.WriteLine(sum.ToString(inv)); // 0.30000000000000004

That is also why you should never compare doubles with == after arithmetic. Compare with a tolerance instead: Math.Abs(a - b) < 1e-9.

Parsing user input

The culture problem works in reverse as well. double.Parse("123.45") on a German system returns 12345, because the dot is read as a thousands separator. Parse machine-written text with CultureInfo.InvariantCulture, and user input with double.TryParse(text, NumberStyles.Float, CultureInfo.CurrentCulture, out double value).

The reference for all specifiers is on Microsoft Learn: Custom numeric format strings.

Related examples

Leave a Comment