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.
- 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?
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?
- 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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…