問題描述
這篇文章是關于 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.
- 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 useXmlConvert.ToDouble();
or parse withInvariantCulture
..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.
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模板網!