浮点数解析:是否有捕获所有的算法? [英] Floating Point Number parsing: Is there a Catch All algorithm?

查看:136
本文介绍了浮点数解析:是否有捕获所有的算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个多元文化的节目的有趣的部分是数字格式。

One of the fun parts of multi-cultural programming is number formats.

  • 在美国使用10,000.50
  • 在德国人使用10.000,50
  • 在法国使用10 000,50

我的第一个办法是采取串,向后分析它,直到我遇到一个分隔符,并以此作为我的小数点分隔符。有与一个明显的缺陷:10.000将PTED 10间$ P $

My first approach would be to take the string, parse it backwards, until I encounter a separator and use this as my decimal separator. There is an obvious flaw with that: 10.000 would be interpreted as 10.

另一种方法:如果字符串包含2个不同的非数字字符,用最后一个作为小数点分隔符,并丢弃等。如果我只有一个,检查它是否出现不止一次,并丢弃它,如果它。如果只出现一次,检查是否有3个数字后。如果是的话,抛弃它,否则使用它作为小数点分隔符。

Another approach: if the string contains 2 different non-numeric characters, use the last one as the decimal separator and discard the others. If I only have one, check if it occurs more than once and discard it if it does. If it only appears once, check if it has 3 digits after it. If yes, discard it, otherwise use it as decimal separator.

明显的最佳解决方案将是检测用户的文化或浏览器,但如果你有一个法国人使用的是EN-US视窗/浏览器不能正常工作。

The obvious "best solution" would be to detect the User's culture or Browser, but that does not work if you have a Frenchman using an en-US Windows/Browser.

请问.NET框架包含了一些神秘的魔​​法浮点解析器比双好。(尝试)解析()在试图自动检测数字格式?

Does the .net Framework contain some mythical black magic floating point parser that is better than Double.(Try)Parse() in trying to auto-detect the number format?

推荐答案

我觉得最好的,你可以在这种情况下,要做的就是把自己的输入,然后告诉他们你的想法,他们的意思。如果他们不同意,告诉他们你希望的格式,让他们再次输入。

I think the best you can do in this case is to take their input and then show them what you think they meant. If they disagree, show them the format you're expecting and get them to enter it again.

这篇关于浮点数解析:是否有捕获所有的算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆