Before I start thank you to Mike Geyser for for bringing this up on Twitter.
Recently I wrote about what is the correct way to display currency in South Africa, which was meant as an interest post rather than anything related to Windows 8 or development – but it seems to be serendipitous in it’s timing. The post referred to the currency format and how you should use a comma to separate Rands & Cents. Windows has always (at least as far back as Windows 98) respected this as the correct currency format in South Africa.
Here you can see Windows 7 (left) and Windows 8 (right) have the same settings.
However with Windows 8, Microsoft have decided to be correct everywhere so if you compare the number format settings in Windows 7 (left) it uses a full stop where Windows 8 (right) uses a comma for number formatting.
The problem is that the number format setting is used in .NET for parsing which means that all numeric code parsing code will break in Windows 8 if you have the South African formatting set!
var testOne = Decimal.Parse("10.2"); // works in Windows 7 and before - exception in Windows 8 var testTwo = Decimal.Parse("10,2"); // works in Windows 8 but fails in Windows 7 and before
The exception that will be raised is: System.FormatException: Input string was not in a correct format.
To be fair, Microsoft has said you should never write code like that, you should always provide the culture settings when parsing (this code always works):
var rightWay = Decimal.Parse("10.2", CultureInfo.InvariantCulture);
There are also two other ways to handle this:
- Add the culture to the config file
- This is likely the easiest route for migrating applications to Windows 8, since it requires no code changes. You can set the culture in the config file to Invariant and the app will respect that.
- How: http://stackoverflow.com/questions/9104084/how-do-i-set-cultureinfo-currentculture-from-an-app-config-file
- Set culture globally
- With .NET 4.5 you can use the new DefaultThreadCurrentCulture once at the start of your app in code and you will be fine through the rest of the code.
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture; var anotherWay = Decimal.Parse("10.2");
In summary when going to Windows 8, make sure you properly test your applications and you test them on the culture that the users will use.