这篇文章是关于 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.
那么我们该如何解决这个问题呢?
还有其他方法可以解决这个问题吗?我可以让应用保持不变吗?就像在不同的环境中启动应用一样.
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:
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.
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
.
.Net app that use CSV files to store input vector as CSV - also throws.
Another .Net app that has some parsing inside the class - throws.
So how can we fix this?
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..
这篇关于如何修复有小数分隔符问题的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!