when displaying currency I am using a method .ToString("C") to display a decimal number in currency format. But the...">
при отображении валюты - c# ⏰ 🌎 🐦

Remove 'US' before '$' when displaying currency

I am using a method .ToString("C")

to display a decimal number in currency format. But the result looks like lke "US $ 5,365.20" instead of "$ 5,365.20" . I tried using .ToString("C", new CultureInfo("en-US"))

, the result is the same. But when I tried to use a different culture (like en-GB) everything was fine ( £ 1,234.56 ).

So why does "US" appear before the "$" character? And how can I remove it?

UPD Sorry. Just checked it again and I can see that new CultureInfo("en-US"))

everything works fine, I looked at a different shortcut :) But now I see (thanks to @Mike McCaughan) that the current currency symbol is "US $". The name of the current culture is "en-US".

+3


source to share


3 answers


The "US" appears before the "$" because someone configured the currency symbol using the Region applet in the Control Panel. To get the default currency symbol, use the constructorCultureInfo.GetCultureInfo

instead :

decimal amount = 5365.20M;
Console.WriteLine(amount.ToString("C"));                                      // US$5,365.20
Console.WriteLine(amount.ToString("C", new CultureInfo("en-US")));            // US$5,365.20
Console.WriteLine(amount.ToString("C", CultureInfo.GetCultureInfo("en-US"))); // $5,365.20

      

The MSDN library documentation explains the difference. For the constructor:

The user can choose to override some of the values ​​associated with the current Windows culture by using the Regional and Language Options in Control Panel .... If the culture ID associated with the name matches the culture ID of the current Windows culture, this constructor creates a CultureInfo object that uses these overrides ...



And for GetCultureInfo:

If name is the name of the current culture, the returned CultureInfo object does not reflect any user overrides. This makes the method suitable for server-side applications or tools that do not have a real user account on the system and need to efficiently load multiple cultures.

UPDATE: If it .ToString("C", new CultureInfo("en-US"))

works but .ToString("C")

doesn't work, the problem might be that some other code assigned your thread a custom CultureInfo with "US $" as the currency symbol.

+6


source


Testing what it provided Mike McCaughan

, I think it will remove US

, but it will add $$

to the existing currency as well. I tested this and it will work, but I think there are some better ways to do this, but this will give the following

Delete US

will initially resolve it on one line.

var currency = "US$5,365.20".Replace("US", string.Empty);

      



or alternative

Taking a suggestion B.K

, you can take this one more level from 3 lines to 2 lines

var currency = 5365.20M;
var finalCurrency = currency.ToString("C", new CultureInfo("en-US")).Replace("US", string.Empty);

      

+2


source


You can change the currency symbol for this specific cloning of the culture culture:

var d = 2353.23M;
var currencyFormat = (NumberFormatInfo)CultureInfo.GetCultureInfo("en-US").NumberFormat.Clone();
currencyFormat.CurrencySymbol = "$"; // Change to something like %^# to test it
var currency = d.ToString("C", currencyFormat);

      

+1


source







All Articles