How to fix an application that has a problem with decimal separator

Lukasz Madon picture Lukasz Madon · Jun 21, 2011 · Viewed 7k times · Source

This post is about C# and .Net, but some info is valuable for other techonolgies.

Since I can remember, I've been having problems with apps or games that crash because of a different style of parsing decimal numbers. It happens very often, from CAD apps, libraries to web pages. I'm not sure whether it is ignorance or lack of knowledge, but it's really annoying.

What's the problem? Here is a wiki article about it, but it short:

Here is a map that shows what kind of decimal separator (decimal mark) is used around the world. map of diffrent decimal separator

Decimal marks:

  • Period — Blue
  • Comma — Green
  • Non-West-Arabic Numerals — Red
  • Unknown — Grey

Most of the Europe, South America write 1 000 000,00 or 1000000,00 sometimes 1.000.000,00 as opposite to "imperial" (marked as blue) write 1,000,000.00

Let me just give you a few from all problems that I encoutered last month.

  1. Number on webpages hard to read: Let's take one of the most viewed YT videos. It shows me 390159851. Number above one million are very hard to read, what's the order of magnitude? Is it 39 mln or 390?
  2. Mirosoft XNA for Windows Phone 7 example: There is a very neat class that parse XML file to produce XNA animation

    /// <summary>
    /// Loads animation setting from xml file.
    /// </summary>
    private void LoadAnimiationFromXML()
    {
        XDocument doc = 
                  XDocument.Load("Content/Textures/AnimationsDefinition.xml");
        XName name = XName.Get("Definition");
        var definitions = doc.Document.Descendants(name);
    
    
            if (animationDefinition.Attribute("Speed") != null)
            {
                animation.SetFrameInvterval(TimeSpan.FromMilliseconds(
                    double.Parse(animationDefinition.Attribute("Speed").Value)));
            }
    

    double.Parse throws an exception, one simple soultion is to use XmlConvert.ToDouble(); or parse with InvariantCulture.

  3. .Net app that use CSV files to store input vector as CSV - also throws.

  4. Another .Net app that has some parsing inside the class - throws.

So how can we fix this?

  • Fix the bug in code - possible only with avaible source code and tedious.
  • Change the data (CVS, XML etc.) - even with possible, not very happy with mutating the data.
  • Change the OS default decimal separator - not gonna happen.

Is there any other way to slove this? Can I make the app be invariant? Like starting the app in a different environment.

PS. I would like to run the app, but I don't have the code.

Answer

codymanix picture codymanix · Jun 21, 2011

Properly set Thread.CurrentThread.CurrentCulture and you wont have such problems. Read here how to write culture independent code.

EDIT: If you do not have access to the code, you can run the application under a user account which has the expected culture set. For quick access you could create an english user, a german user, a french user..