Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the Currency via code in c#

Tags:

c#

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

like image 763
Coppermill Avatar asked Jun 19 '09 09:06

Coppermill


People also ask

How do I change the currency format?

On the Home tab, click the Dialog Box Launcher next to Number. Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want.

How do I change the default currency in C#?

Choose File > Options > Display. In the Symbol box, type the symbol of the currency that you want to use. In the Placement box, select the position that you want the currency symbol to occupy.

How do you represent currency?

If the currency type is in question, the symbol may include a country prefix, such as CA$, Can$ or C$ for Canadian dollars, or US$ for U.S. dollars. The € symbol is used to represent the euro currency, ¥ represents the Japanese yen, and ƒ signifies a florin, such as the Aruban Florin.


1 Answers

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);
like image 156
Thomas Levesque Avatar answered Oct 30 '22 06:10

Thomas Levesque