Currency Formatting

suggest change

The ā€œcā€ (or currency) format specifier converts a number to a string that represents a currency amount.

string.Format("{0:c}", 112.236677) // $112.23 - defaults to system

Precision

Default is 2. Use c1, c2, c3 and so on to control precision.

string.Format("{0:C1}", 112.236677) //$112.2
string.Format("{0:C3}", 112.236677) //$112.237
string.Format("{0:C4}", 112.236677) //$112.2367
string.Format("{0:C9}", 112.236677) //$112.236677000

Currency Symbol

  1. Pass CultureInfo instance to use custom culture symbol.
    string.Format(new CultureInfo("en-US"), "{0:c}", 112.236677); //$112.24
    string.Format(new CultureInfo("de-DE"), "{0:c}", 112.236677); //112,24 €
    string.Format(new CultureInfo("hi-IN"), "{0:c}", 112.236677); //₹ 112.24
  2. Use any string as currency symbol. Use NumberFormatInfo as to customize currency symbol.
    NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
    nfi = (NumberFormatInfo) nfi.Clone();
    nfi.CurrencySymbol = "?";
    string.Format(nfi, "{0:C}", 112.236677); //?112.24
    nfi.CurrencySymbol = "?%^&";
    string.Format(nfi, "{0:C}", 112.236677); //?%^&112.24

Position of Currency Symbol

Use CurrencyPositivePattern for positive values and CurrencyNegativePattern for negative values.

NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;        
nfi.CurrencyPositivePattern = 0;
string.Format(nfi, "{0:C}", 112.236677); //$112.24 - default
nfi.CurrencyPositivePattern = 1;
string.Format(nfi, "{0:C}", 112.236677); //112.24$
nfi.CurrencyPositivePattern = 2;
string.Format(nfi, "{0:C}", 112.236677); //$ 112.24
nfi.CurrencyPositivePattern = 3; 
string.Format(nfi, "{0:C}", 112.236677); //112.24 $

Negative pattern usage is the same as positive pattern. A lot more use cases please refer to original link.

Custom Decimal Separator

NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;        
nfi.CurrencyPositivePattern = 0;
nfi.CurrencyDecimalSeparator = "..";
string.Format(nfi, "{0:C}", 112.236677); //$112..24

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


String Format:
* Currency Formatting

Table Of Contents
11 String Format
17 Regex
19 Arrays
21 Enum
22 Tuples
24 GUID
27 Looping
36 Casting
46 Methods
88 Events
92 Structs
104 Indexer
106 Stream
107 Timers
109 Threading
127 Caching
135 Pointers
147 C# Script