In a previous .Net life, the way that I would format a currency (any currency) for the current language would be to do something like this:
public string FormatCurrencyValue(string symbol, decimal val)
{
var format = (NumberFormatInfo)CultureInfo.CurrentUICulture.NumberFormat.Clone();
//overwrite the currency symbol with the one I want to display
format.CurrencySymbol = symbol;
//pass the format to ToString();
return val.ToString("{0:C2}", format);
}
This returns the currency value, without any decimal parts, formatted for the given currency symbol, adjusted for the current culture – e.g. £50.00 for en-GB but 50,00£ for fr-FR.
The same code running under Windows Store produces {50:C}.
Looking at the (rather terrible) WinRT documentation, we do have the CurrencyFormatter class – but it was only after trying to fire the constructor with "£" as the parameter, and getting an ArgumentException (WinRT documentation is so special – it has practically no information about exceptions) that I realised it wanted an ISO currency symbol (in fairness the parameter name is currencyCode, but even so).
Now – I can get one of those as well, but the CurrencyFormatter has another issue that makes it unsuitable for currency formatting – you can only format double, long and ulong types – there’s no decimal overload – which can make for some interesting value errors in some situations.
So how to format currencies dynamically in WinRT.net?
I’ve found that you can still use old-style format strings with the
NumberFormatInfoclass – it’s just that, inexplicably, it doesn’t work when you useToString. If you useString.Formatinstead, then it works.So we can rewrite the code in my question to:
Which gives the desired result.