如何修复有小数分隔符问题的应用程序

How to fix an application that has a problem with decimal separator(如何修复有小数分隔符问题的应用程序)

本文介绍了如何修复有小数分隔符问题的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章是关于 C# 和 .Net 的,但有些信息对其他技术很有价值.

从我记事起,我就一直遇到应用程序或游戏因解析十进制数的不同方式而崩溃的问题.它经常发生,从 CAD 应用程序、库到网页.不知道是无知还是缺乏见识,但真的很烦人.

有什么问题?.它告诉我390159851.超过一百万的数字很难阅读,数量级是多少?是 3900 万还是 390?

  • Mirosoft XNA for Windows Phone 7 示例:有是一个非常简洁的类,可以解析 XML 文件以生成 XNA 动画

    ///<总结>///从 xml 文件加载动画设置.///</总结>私人无效 LoadAnimiationFromXML(){XDocument 文档 =XDocument.Load("内容/纹理/AnimationsDefinition.xml");XName 名称 = XName.Get("定义");var 定义 = doc.Document.Descendants(name);if (animationDefinition.Attribute("Speed") != null){animation.SetFrameInvterval(TimeSpan.FromMilliseconds(double.Parse(animationDefinition.Attribute("Speed").Value)));}

    double.Parse 抛出异常,一个简单的方法是使用 XmlConvert.ToDouble(); 或使用 InvariantCulture 解析.

  • .Net 应用程序使用 CSV 文件将输入向量存储为 CSV - 也会抛出.

  • 另一个在类中有一些解析的 .Net 应用程序 - throws.

  • 那么我们该如何解决这个问题呢?

    • 修复代码中的错误 - 只能使用可用的源代码且繁琐.
    • 更改数据(CVS、XML 等) - 即使有可能,但对更改数据不太满意.
    • 更改操作系统默认的小数点分隔符 - 不会发生.

    还有其他方法可以解决这个问题吗?我可以让应用保持不变吗?就像在不同的环境中启动应用一样.

    PS.我想运行应用程序,但我没有代码.

    解决方案

    正确设置Thread.CurrentThread.CurrentCulture就不会出现这样的问题.阅读此处如何编写与文化无关的代码.

    如果您无权访问代码,则可以在具有预期文化集的用户帐户下运行应用程序.为了快速访问,您可以创建一个英语用户、一个德语用户、一个法语用户..

    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.

    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.

    解决方案

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

    这篇关于如何修复有小数分隔符问题的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:如何修复有小数分隔符问题的应用程序

    基础教程推荐