Change currency through code in C #

To display the amount, I use the following:

String.Format ("{0: C}", item.Amount)

This display is Β£ 9.99.

which is good, but what if I want the application to be able to control the currency and be able to change the currency for the day

$ 9.99

How to change currency format using code

+4
source share
4 answers

Specify the culture in the Format call:

  decimal value = 123.45M; CultureInfo us = CultureInfo.GetCultureInfo("en-US"); string s = string.Format(us, "{0:C}", value); 
+4
source

The currency symbol is determined by CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol. The property is read / written, but you will probably get an exception if you try to change it, because NumberFormatInfo.IsReadOnly will be true ...

Alternatively, you can format the number explicitly using a specific NumberFormatInfo:

 NumberFormatInfo nfi = (NumberFormatInfo)CultureInfo.CurrentCulture.NumberFormat.Clone(); nfi.CurrencySymbol = "$"; String.Format(nfi, "{0:C}", item.Amount); 
+13
source
 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)); 
+1
source

If you change the displayed amount currency, you will also change your unit of measure. Β£ 1 <> € 1 <> $ 1. Are you absolutely sure of the business requirements here?

-1
source

Source: https://habr.com/ru/post/1286457/


All Articles