Remove 'US' before '$' in currency displaying

1.8k views Asked by At

I'm using .ToString("C") method to show decimal number in currency format. But result looks lke "US$5,365.20" instead of "$5,365.20". I've tried to use .ToString("C", new CultureInfo("en-US")), results the same. But when I've tried to use another culture (en-GB, for example) then everything was ok (£1,234.56).

So, why "US" is appeared before "$" symbol? And how can I remove it?

UPD I'm sorry. Just checked it again, and i see, that with new CultureInfo("en-US")) all works perfectly, i looked on another label :) But now i see(thanks to @Mike McCaughan), that current currency symbol is "US$". Current culture name is 'en-US'.

3

There are 3 answers

3
Michael Liu On BEST ANSWER

"US" appears before "$" because someone customized the currency symbol using the Region applet in Control Panel. To get the default currency symbol, use CultureInfo.GetCultureInfo instead of the constructor:

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 might choose to override some of the values associated with the current culture of Windows through the regional and language options portion of Control Panel. ... If the culture identifier associated with name matches the culture identifier of the current Windows culture, this constructor creates a CultureInfo object that uses those 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 applications or tools that do not have a real user account on the system and that need to load multiple cultures efficiently.

UPDATE: If .ToString("C", new CultureInfo("en-US")) works but .ToString("C") doesn't, then the problem could be that some other code assigned your thread a custom CultureInfo with "US$" as the currency symbol.

3
MethodMan On

Testing what Mike McCaughan has provided I think that it will remove the US but it will also add $$ to the existing Currency. I have tested this and this will work, but I think that there are some better ways of doing this but this will yield the following

Remove the US initially would solve this in one line..

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

or Alternative

Taking B.K suggestion you can take this even 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);
0
B.K. On

You can change the currency symbol for that specific culture's shallow clone:

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);