That was interesting! Try this:
using System;
using System.Globalization;
using System.Linq;
namespace ConsoleApp {
internal class Program {
static void Main(string[] args) {
Console.WriteLine(GetPrice(123.43, "CAD"));
Console.WriteLine(GetPrice(123.45, "JPY"));
Console.WriteLine(GetPrice(123.47, "USD"));
Console.WriteLine(GetPrice(123.49, "XYZ"));
Console.ReadLine();
}
public static string GetPrice(double price, string currency) {
RegionInfo region = CultureInfo
.GetCultures(CultureTypes.SpecificCultures)
.Select(ct => new RegionInfo(ct.LCID))
.Where(ri=>ri.ISOCurrencySymbol==currency).FirstOrDefault();
if (region != null) {
return string.Format("{0} {1}", price, region.CurrencySymbol);
} else {
return string.Format("{0} {1} (unknown currency!)",
price, currency);
}
}
}
}
Notes:
- it only works for the currencies known to Windows (currently 100+), which may depend on your Windows version.
- it does not work for cryptocurrencies.
- many cultures expect their currency symbol in front of the number, I don't have a general solution for this.
:)