Skip to main content

How to Format a Number with a Custom Format String Like 0.00 in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

distance.ToString("0.00", invariant) turns 3.14159 into 3.14. Each 0 in the format is a digit that always shows, and the . says where the decimal separator goes.

Microsoft's guide: You can create a custom numeric format string, which consists of one or more custom numeric specifiers, to define how to format numeric data. A custom numeric format string is any format string that is not a standard numeric format string.

A standard format string is a letter with an optional number, such as N2. A custom one is a pattern you draw yourself, digit by digit, such as 0.00.

Syntax​

Syntax
string <text> = <number>.ToString("<format>", <culture>);
PartWhat it is
<number>The number to format, such as a double, a decimal or an int
<format>The pattern, made of the characters below
0 in <format>A digit that always shows: the number's digit in that place, or a zero
# in <format>A digit that shows only when the number has one in that place
. in <format>Where the decimal separator goes
<culture>The culture that decides the separator, such as CultureInfo.InvariantCulture

The guide on 0: If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string. And on #, when the number has no digit in that place: Otherwise, nothing is stored in that position in the result string.

The Example​

A console app called NumberFormat formats 3.14159, 2.5, 7 and 42 with four custom format strings in the invariant culture, and formats 3.14159 once more for Germany.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n NumberFormat
cd NumberFormat
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/NumberFormat/NumberFormat.csproj:
Determining projects to restore...
Restored /Users/you/source/NumberFormat/NumberFormat.csproj (in 42 ms).
Restore succeeded.

dotnet new console -n NumberFormat creates a folder named NumberFormat with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or NumberFormat.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the culture and the numbers:

Program.cs (the numbers)
using System.Globalization;

CultureInfo invariant = CultureInfo.InvariantCulture;
double distance = 3.14159;
double price = 2.5;
double whole = 7;
int ticket = 42;

CultureInfo.InvariantCulture is a culture that belongs to no country. Its reference: invariant culture data is stable over time and across installed cultures and cannot be customized by users.

Then the formats:

Program.cs (the formats)
// 0 is a digit that always shows. Extra decimals are rounded.
Console.WriteLine($"3.14159 with 0.00: {distance.ToString("0.00", invariant)}");
Console.WriteLine($"3.14159 with 0.0000: {distance.ToString("0.0000", invariant)}");
Console.WriteLine($"2.5 with 0.00: {price.ToString("0.00", invariant)}");

// # is a digit that shows only when the number has one there.
Console.WriteLine($"2.5 with 0.##: {price.ToString("0.##", invariant)}");
Console.WriteLine($"7 with 0.##: {whole.ToString("0.##", invariant)}");

// Extra 0s before the point add leading zeros.
Console.WriteLine($"42 with 0000: {ticket.ToString("0000", invariant)}");

Each line prints the number and the format as plain text, then the result of ToString.

Then the German culture:

Program.cs (the German culture)
// The . stands for the culture's decimal separator.
CultureInfo german = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine($"3.14159 with 0.00 in de-DE: {distance.ToString("0.00", german)}");

CultureInfo.GetCultureInfo("de-DE") gets German as written in Germany, where a comma separates the decimals.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
3.14159 with 0.00: 3.14
3.14159 with 0.0000: 3.1416
2.5 with 0.00: 2.50
2.5 with 0.##: 2.5
7 with 0.##: 7
42 with 0000: 0042
3.14159 with 0.00 in de-DE: 3,14

macOS-style terminal window titled Terminal — NumberFormat showing dotnet run printing 3.14 and 3.1416 for 3.14159, 2.50 and 2.5 for 2.5, 7 for 7, 0042 for 42, and 3,14 for 3.14159 in de-DE

  • 0.00 gave 3.14 and 0.0000 gave 3.1416: the number was rounded to the decimals the format asks for. The guide: numbers are rounded to as many decimal places as there are digit placeholders to the right of the decimal point.
  • 0.00 turned 2.5 into 2.50: the second 0 showed a zero. With 0.##, 2.5 stayed 2.5, and 7 came out as plain 7, without a decimal point.
  • 0000 gave 42 two leading zeros: 0042.
  • In German, the same 0.00 gave 3,14. The guide: The character that is used as the decimal separator in the result string is not always a period; it is determined by the NumberDecimalSeparator property of the NumberFormatInfo object that controls formatting.

The Complete File​

macOS-style code window titled Program.cs — NumberFormat showing the invariant culture, four numbers, Console.WriteLine calls that format them with 0.00, 0.0000, 0.## and 0000, and a last call that formats 3.14159 with 0.00 for the de-DE culture

Program.cs
using System.Globalization;

CultureInfo invariant = CultureInfo.InvariantCulture;
double distance = 3.14159;
double price = 2.5;
double whole = 7;
int ticket = 42;

// 0 is a digit that always shows. Extra decimals are rounded.
Console.WriteLine($"3.14159 with 0.00: {distance.ToString("0.00", invariant)}");
Console.WriteLine($"3.14159 with 0.0000: {distance.ToString("0.0000", invariant)}");
Console.WriteLine($"2.5 with 0.00: {price.ToString("0.00", invariant)}");

// # is a digit that shows only when the number has one there.
Console.WriteLine($"2.5 with 0.##: {price.ToString("0.##", invariant)}");
Console.WriteLine($"7 with 0.##: {whole.ToString("0.##", invariant)}");

// Extra 0s before the point add leading zeros.
Console.WriteLine($"42 with 0000: {ticket.ToString("0000", invariant)}");

// The . stands for the culture's decimal separator.
CultureInfo german = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine($"3.14159 with 0.00 in de-DE: {distance.ToString("0.00", german)}");

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the cultureThe result depends on the computer: with the current culture set to de-DE, distance.ToString("0.00") gave 3,14Pass a culture. For text that programs read back, use CultureInfo.InvariantCulture, which gave 3.14. Microsoft's rule CA1305: If the value will be stored and accessed by software (persisted to a file or database), use the invariant culture.
Using # before the decimal point0.5 with #.## gave .5. The guide: # never displays a zero that is not a significant digit, even if zero is the only digit in the string.Put a 0 before the point: 0.## gave 0.5
Writing a comma for the decimals, as in 0,003.14159 became 003. The guide: The "," character serves as both a group separator and a number scaling specifier. With no ., the number is rounded to the nearest integer.Write 0.00, and let the culture choose the separator

FAQ​

Can I use a custom format in string interpolation?​

Yes, after a colon: $"{distance:0.00}". The guide: String interpolation feature also supports custom numeric format strings. It uses the current culture, so with de-DE as the current culture it printed 3,14. For text that is the same everywhere, string.Create(CultureInfo.InvariantCulture, $"{distance:0.00}") printed 3.14.

How do I add thousands separators?​

Put a comma between digit placeholders before the point: 1234567.891 with #,##0.00 gave 1,234,567.89. The guide: If one or more commas are specified between two digit placeholders (0 or #) that format the integral digits of a number, a group separator character is inserted between each number group in the integral part of the output.

Conclusion​

Draw the number you want with 0 for digits that always show, # for digits that show only when they're there, and . for the decimal separator, then pass a culture: ToString("0.00", CultureInfo.InvariantCulture). The . turns into the culture's separator, so the culture belongs in the call.

Additional Resources​