Is there a way to format the correct currency representation for a country?
Example UK -£127.54 Netherlands € 127,54- USA $127.54
etc..
Some things to
Try the Currency Format Specifier ("C"). It automatically takes the current UI culture into account and displays currency values accordingly.
You can use it with either String.Format or the overloaded ToString
method for a numeric type.
For example:
double value = 12345.6789;
Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));
Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));
Console.WriteLine(value.ToString("C3", CultureInfo.CreateSpecificCulture("da-DK")));
// The example displays the following output on a system whose
// current culture is English (United States):
// $12,345.68
// $12,345.679
// kr 12.345,679
You can use string.Format("{0:c}", value)
.
See also here:
public static string ToFormattedCurrencyString(
this decimal currencyAmount,
string isoCurrencyCode,
CultureInfo userCulture)
{
var userCurrencyCode = new RegionInfo(userCulture.Name).ISOCurrencySymbol;
if (userCurrencyCode == isoCurrencyCode)
{
return currencyAmount.ToString("C", userCulture);
}
return string.Format(
"{0} {1}",
isoCurrencyCode,
currencyAmount.ToString("N2", userCulture));
}
If you just have the currency symbol and the number of decimal places, you can use the following helper function, which respects the symbol/amount order, separators etc, only changing the currency symbol itself and the number of decimal places to display to.
public static string FormatCurrency(string currencySymbol, Decimal currency, int decPlaces)
{
NumberFormatInfo localFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
localFormat.CurrencySymbol = currencySymbol;
localFormat.CurrencyDecimalDigits = decPlaces;
return currency.ToString("c", localFormat);
}
This kind of functionality is built in.
When using a decimal you can use a format string "C" or "c".
decimal dec = 123.00M;
string uk = dec.ToString("C", new CultureInfo("en-GB")); // uk holds "£123.00"
string us = dec.ToString("C", new CultureInfo("en-US")); // us holds "$123.00"
The problem with taking a given number and displaying it with .ToString("C", culture) is that it effectively changes the amount to the default currency of the given culture. If you have a given amount, the ISO currency code of that amount, and you want to display it for a given culture, I would recommend just creating a decimal extension method like the one below. This will not automatically assume that the currency is in the default currency of the culture:
public static string ToFormattedCurrencyString(
this decimal currencyAmount,
string isoCurrencyCode,
CultureInfo userCulture)
{
var userCurrencyCode = new RegionInfo(userCulture.Name).ISOCurrencySymbol;
if (userCurrencyCode == isoCurrencyCode)
{
return currencyAmount.ToString("C", userCulture);
}
return string.Format(
"{0} {1}",
isoCurrencyCode,
currencyAmount.ToString("N2", userCulture));
}
This will either use the local currency symbol or the ISO currency code with the amount -- whichever is more appropriate. More on the topic in this blog post.