Reputation: 47
A bit of background - I am writing some automated user acceptance tests for a mobile application and I don't always have control over the culture of the device that the tests will be executed on.
This application deals with currencies and I was wondering what the best approach might be to parse a string with a currency amount reliably without knowing the device culture.
For example, I would like to parse €12.34 or $12.34 or £12.34 etc to a double value (or whatever).
A workaround is to ignore the first character in the string but that's not necessarily an ideal solution.
Upvotes: 0
Views: 700
Reputation: 47
What I did eventually to address this was to get all possible international currency symbols as per the snippet below (thanks to stuard) and then to check my own string against each of these values.
string[] currencySymbols = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures).Select(culture => culture.NumberFormat.CurrencySymbol) .Distinct() .ToArray();
I stripped any numeric characters, '.' and ',' from my own string, leaving just leave the currency prefix, before doing the comparison.
I'm not sure if it's the best possible solution but it works!
Upvotes: 1