Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
String format for Double in C#
The String.Format method in C# provides powerful options for formatting double values. You can control decimal places, add thousands separators, and apply various numeric formatting patterns to display doubles exactly as needed.
Syntax
Following is the basic syntax for formatting double values −
String.Format("{0:format_specifier}", double_value)
Common format specifiers for doubles −
{0:0.000} // Fixed decimal places
{0:0,0.0} // Thousands separator with decimal
{0:F2} // Fixed-point notation with 2 decimals
{0:N2} // Number format with thousands separator
Using Fixed Decimal Places
You can specify exactly how many decimal places to display using the 0.000 pattern −
using System;
class Demo {
public static void Main(string[] args) {
Console.WriteLine("Three decimal places...");
Console.WriteLine(String.Format("{0:0.000}", 987.383));
Console.WriteLine(String.Format("{0:0.000}", 987.38));
Console.WriteLine(String.Format("{0:0.000}", 987.7899));
Console.WriteLine("\nTwo decimal places...");
Console.WriteLine(String.Format("{0:0.00}", 123.4));
Console.WriteLine(String.Format("{0:0.00}", 123.456));
}
}
The output of the above code is −
Three decimal places... 987.383 987.380 987.790 Two decimal places... 123.40 123.46
Using Thousands Separator
The thousands separator can be added using the 0,0 pattern −
using System;
class Demo {
public static void Main(string[] args) {
Console.WriteLine("Thousands separator with decimal...");
Console.WriteLine(String.Format("{0:0,0.0}", 54567.46));
Console.WriteLine(String.Format("{0:0,0.00}", 1234567.89));
Console.WriteLine("\nThousands separator without decimal...");
Console.WriteLine(String.Format("{0:0,0}", 54567.46));
Console.WriteLine(String.Format("{0:0,0}", 1234567.89));
}
}
The output of the above code is −
Thousands separator with decimal... 54,567.5 1,234,567.89 Thousands separator without decimal... 54,567 1,234,568
Using Standard Format Specifiers
C# also provides standard format specifiers like F (Fixed-point) and N (Number) −
using System;
class Demo {
public static void Main(string[] args) {
double value = 1234567.89;
Console.WriteLine("Fixed-point notation:");
Console.WriteLine(String.Format("{0:F2}", value));
Console.WriteLine(String.Format("{0:F0}", value));
Console.WriteLine("\nNumber format:");
Console.WriteLine(String.Format("{0:N2}", value));
Console.WriteLine(String.Format("{0:N0}", value));
Console.WriteLine("\nCurrency format:");
Console.WriteLine(String.Format("{0:C2}", value));
}
}
The output of the above code is −
Fixed-point notation: 1234567.89 1234568 Number format: 1,234,567.89 1,234,568 Currency format: $1,234,567.89
Format Specifier Comparison
| Format Specifier | Description | Example Output |
|---|---|---|
| {0:0.000} | Custom fixed decimal places | 123.450 |
| {0:F2} | Fixed-point with 2 decimals | 123.45 |
| {0:N2} | Number with thousands separator | 1,234.57 |
| {0:0,0} | Custom thousands separator | 1,235 |
Conclusion
The String.Format method provides flexible formatting options for double values in C#. Use custom patterns like {0:0.000} for precise decimal control, or standard specifiers like F and N for common formatting needs including thousands separators and currency display.
