Reputation: 6794
I am using the following to display an amount:
String.Format("{0:C}", item.Amount)
This display £9.99
which is okay, but what if I want the application to be able to control the currency and to be able to change the currency to day
$9.99
How do I change the currency format via code
Upvotes: 7
Views: 17572
Reputation: 31071
If you change the displayed currency of an amount, then you are changing its unit of measurement too. £1 <> €1 <> $1. Are you absolutely sure of the business requirement here?
Upvotes: -1
Reputation: 292405
The currency symbol is defined by CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol. The property is read/write but you will probably get an exception if you try to change it, because NumberFormatInfo.IsReadOnly will be true...
Alternatively, you could format the number by explicitly using a specific NumberFormatInfo :
NumberFormatInfo nfi = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone();
nfi.CurrencySymbol = "$";
String.Format(nfi, "{0:C}", item.Amount);
Upvotes: 18
Reputation: 56934
CultureInfo info = new CultureInfo (System.Threading.Thread.CurrentThread.CurrentCulture.LCID);
info.NumberFormat.CurrencySymbol = "EUR";
System.Threading.Thread.CurrentThread.CurrentCulture = info;
Console.WriteLine (String.Format ("{0:C}", 45M));
or
NumberFormatInfo info = new NumberFormatInfo ();
info.CurrencySymbol = "EUR";
Console.WriteLine (String.Format (info, "{0:C}", 45M));
Upvotes: 2
Reputation: 1062695
Specify the culture in the call to Format
:
decimal value = 123.45M;
CultureInfo us = CultureInfo.GetCultureInfo("en-US");
string s = string.Format(us, "{0:C}", value);
Upvotes: 4