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
How to use String Format to show decimal up to 2 places or simple integer in C#?
The String.Format method in C# converts values to formatted strings and inserts them into another string. It's particularly useful for displaying numbers with consistent decimal places, showing integers as decimals when needed, or keeping integers without unnecessary decimal points.
Syntax
Following is the syntax for String.Format with decimal formatting −
string.Format("{0:format}", value)
Common decimal format specifiers −
"{0:0.00}" // Always shows 2 decimal places
"{0:F2}" // Fixed-point with 2 decimal places
"{0:N2}" // Number format with 2 decimal places and thousand separators
Using String.Format for Decimal Formatting
Example
using System;
class Program {
static void Main(string[] args) {
int wholeNumber = 123;
double decimalNumber = 123.456;
// Format integer to show 2 decimal places
var formatted1 = string.Format("{0:0.00}", wholeNumber);
Console.WriteLine("Integer as decimal: " + formatted1);
// Format decimal to 2 places
var formatted2 = string.Format("{0:0.00}", decimalNumber);
Console.WriteLine("Decimal rounded: " + formatted2);
// Using F2 format specifier
var formatted3 = string.Format("{0:F2}", decimalNumber);
Console.WriteLine("Using F2 format: " + formatted3);
}
}
The output of the above code is −
Integer as decimal: 123.00 Decimal rounded: 123.46 Using F2 format: 123.46
Using String Interpolation
String interpolation with the $ symbol provides a more readable syntax for the same formatting −
Example
using System;
class Program {
static void Main(string[] args) {
int number = 123;
double price = 45.8;
// String interpolation with decimal formatting
var formatted1 = $"{number:0.00}";
Console.WriteLine("Integer formatted: " + formatted1);
var formatted2 = $"{price:0.00}";
Console.WriteLine("Price formatted: " + formatted2);
// Conditional formatting - show decimals only if needed
var smart1 = $"{number:0.##}";
var smart2 = $"{price:0.##}";
Console.WriteLine("Smart format (int): " + smart1);
Console.WriteLine("Smart format (decimal): " + smart2);
}
}
The output of the above code is −
Integer formatted: 123.00 Price formatted: 45.80 Smart format (int): 123 Smart format (decimal): 45.8
Common Format Specifiers
| Format | Description | Example (123.456) |
|---|---|---|
| 0.00 | Always shows 2 decimal places | 123.46 |
| 0.## | Shows up to 2 decimal places, hides trailing zeros | 123.46 |
| F2 | Fixed-point notation with 2 decimal places | 123.46 |
| N2 | Number format with thousand separators | 123.46 |
Practical Example with Mixed Data Types
Example
using System;
class Program {
static void Main(string[] args) {
int quantity = 5;
double unitPrice = 12.5;
double total = quantity * unitPrice;
Console.WriteLine("Order Summary:");
Console.WriteLine($"Quantity: {quantity}");
Console.WriteLine($"Unit Price: ${unitPrice:0.00}");
Console.WriteLine($"Total: ${total:0.00}");
// Using String.Format method
string receipt = string.Format("Receipt: {0} items × ${1:0.00} = ${2:0.00}",
quantity, unitPrice, total);
Console.WriteLine(receipt);
}
}
The output of the above code is −
Order Summary: Quantity: 5 Unit Price: $12.50 Total: $62.50 Receipt: 5 items × $12.50 = $62.50
Conclusion
String formatting in C# allows precise control over decimal display using format specifiers like 0.00 for fixed decimal places or 0.## for optional decimals. Both String.Format and string interpolation provide flexible ways to format numbers consistently in your applications.
