C# Currency ("C") Format Specifier
Description
The "C" (or currency) format specifier formats a number as currency.
Item | Value |
---|---|
Format specifier | "C" or "c" |
Supported Type | All numeric types |
Result | A currency value. |
Precision specifier | Number of decimal digits. The default precision is defined by the NumberFormatInfo.CurrencyDecimalDigits property. |
Default precision specifier | Defined by System.Globalization.NumberFormatInfo. |
Examples:
Value | Format | Formatted |
---|---|---|
123.456 | ("C", en-US) | $123.46 |
-123.456 | ("C3", en-US) | ($123.456) |
If the value has more than the specified or default number of decimal places, the fractional value is rounded in the result string.
The following table lists the NumberFormatInfo
properties
that control the formatting of the returned string.
- CurrencyPositivePattern
- CurrencyNegativePattern
- CurrencySymbol
- CurrencyDecimalDigits
- CurrencyDecimalSeparator
- CurrencyGroupSeparator
- CurrencyGroupSizes
- NegativeSign
Example
using System;/*from w ww. j ava 2 s .c o m*/
using System.Globalization;
class MainClass
{
static void Main()
{
double value = 12345.6789;
Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));
Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));
Console.WriteLine(value.ToString("C3", CultureInfo.CreateSpecificCulture("da-DK")));
}
}
The code above generates the following result.