Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...