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
C# Currency ("C") Format Specifier
The C format specifier (currency) is used to format numeric values as currency amounts. It automatically applies the appropriate currency symbol, decimal places, and thousands separators based on the current culture settings.
Syntax
Following is the syntax for using the currency format specifier −
value.ToString("C") // Default currency format
value.ToString("C3") // Currency with 3 decimal places
value.ToString("C", culture) // Currency with specific culture
The precision specifier (optional number after C) determines how many decimal places to display. If not specified, it uses the culture's default currency decimal places.
Parameters
Precision Specifier − Optional number (0-99) that specifies the number of decimal places.
CultureInfo − Optional culture parameter that determines currency symbol and formatting rules.
Using Currency Format with Default Settings
The basic currency format uses the system's current culture settings −
using System;
using System.Globalization;
class Demo {
static void Main() {
double value = 234.66;
// Default currency format (2 decimal places in US)
Console.WriteLine(value.ToString("C"));
// Currency with 3 decimal places
Console.WriteLine(value.ToString("C3"));
// Currency with 0 decimal places
Console.WriteLine(value.ToString("C0"));
}
}
The output of the above code is −
$234.66 $234.660 $235
Using Currency Format with Different Cultures
Different cultures use different currency symbols and formatting conventions −
using System;
using System.Globalization;
class CurrencyExample {
static void Main() {
double amount = 1234.56;
// US culture (default)
Console.WriteLine("US: " + amount.ToString("C", new CultureInfo("en-US")));
// European cultures
Console.WriteLine("Germany: " + amount.ToString("C", new CultureInfo("de-DE")));
Console.WriteLine("France: " + amount.ToString("C", new CultureInfo("fr-FR")));
// Asian cultures
Console.WriteLine("Japan: " + amount.ToString("C", new CultureInfo("ja-JP")));
Console.WriteLine("India: " + amount.ToString("C", new CultureInfo("hi-IN")));
}
}
The output of the above code is −
US: $1,234.56 Germany: 1.234,56 ? France: 1 234,56 ? Japan: ¥1,235 India: ?1,234.56
Currency Format Behavior
| Feature | Description |
|---|---|
| Currency Symbol | Automatically added based on culture ($, ?, ¥, ?, etc.) |
| Thousands Separator | Added according to culture rules (comma, period, space) |
| Decimal Places | Culture default (usually 2) or specified precision |
| Negative Values | Formatted according to culture (parentheses, minus sign) |
Handling Negative Currency Values
Currency formatting handles negative values differently based on culture −
using System;
using System.Globalization;
class NegativeCurrency {
static void Main() {
double negativeAmount = -567.89;
Console.WriteLine("US negative: " + negativeAmount.ToString("C", new CultureInfo("en-US")));
Console.WriteLine("UK negative: " + negativeAmount.ToString("C", new CultureInfo("en-GB")));
Console.WriteLine("Germany negative: " + negativeAmount.ToString("C", new CultureInfo("de-DE")));
}
}
The output of the above code is −
US negative: ($567.89) UK negative: -£567.89 Germany negative: -567,89 ?
Conclusion
The C format specifier provides automatic currency formatting based on culture settings, including appropriate currency symbols, decimal places, and thousands separators. It's essential for displaying monetary values in applications that support multiple cultures and currencies.
