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
Formatted output in C#
In C#, formatted output allows you to control how data is displayed when converting values to strings. This is essential for presenting numbers, dates, and other data types in a user-friendly format using format specifiers and the String.Format() method.
Syntax
Following is the syntax for basic string formatting −
String.Format("{index:format}", value)
Where index is the parameter position (starting from 0) and format is the format specifier.
Using Format Specifiers for Numbers
Decimal Places and Thousands Separator
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("Thousands Separator...");
Console.WriteLine(String.Format("{0:0,0.0}", 54567.46));
Console.WriteLine(String.Format("{0:0,0}", 54567.46));
}
}
The output of the above code is −
Three decimal places... 987.383 987.380 987.790 Thousands Separator... 54,567.5 54,567
Currency and Percentage Formatting
using System;
class Demo {
public static void Main(String[] args) {
double price = 1234.56;
double percentage = 0.75;
Console.WriteLine("Currency format: " + String.Format("{0:C}", price));
Console.WriteLine("Percentage format: " + String.Format("{0:P}", percentage));
Console.WriteLine("Fixed-point format: " + String.Format("{0:F2}", price));
Console.WriteLine("Number format: " + String.Format("{0:N}", price));
}
}
The output of the above code is −
Currency format: $1,234.56 Percentage format: 75.00% Fixed-point format: 1234.56 Number format: 1,234.56
Using Format Specifiers for DateTime
using System;
static class Demo {
static void Main() {
DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123);
Console.WriteLine(String.Format("{0:y yy yyy yyyy}", d));
Console.WriteLine(String.Format("{0:M MM MMM MMMM}", d));
Console.WriteLine(String.Format("{0:d dd ddd dddd}", d));
Console.WriteLine(String.Format("{0:h hh H HH}", d));
Console.WriteLine(String.Format("{0:m mm s ss}", d));
}
}
The output of the above code is −
18 18 2018 2018 2 02 Feb February 8 08 Thu Thursday 12 12 12 12 7 07 7 07
Standard DateTime Format Strings
using System;
class Demo {
public static void Main() {
DateTime now = DateTime.Now;
Console.WriteLine("Short date: " + String.Format("{0:d}", now));
Console.WriteLine("Long date: " + String.Format("{0:D}", now));
Console.WriteLine("Short time: " + String.Format("{0:t}", now));
Console.WriteLine("Long time: " + String.Format("{0:T}", now));
Console.WriteLine("Full date/time: " + String.Format("{0:F}", now));
}
}
The output of the above code is −
Short date: 12/25/2023 Long date: Monday, December 25, 2023 Short time: 3:45 PM Long time: 3:45:30 PM Full date/time: Monday, December 25, 2023 3:45:30 PM
Common Format Specifiers
| Format Specifier | Description | Example |
|---|---|---|
| C or c | Currency format | $1,234.56 |
| F or f | Fixed-point format | 1234.56 |
| N or n | Number format with thousands separator | 1,234.56 |
| P or p | Percentage format | 75.00% |
| 0.000 | Custom decimal places | 123.456 |
Conclusion
Formatted output in C# provides precise control over how data is displayed using format specifiers with String.Format(). Whether formatting numbers with specific decimal places, adding thousands separators, or displaying dates in various formats, these tools ensure your output is professional and user-friendly.
