Android:将欧盟货币解析为数字
我有一个 EditText,它应该按如下方式工作(Android 2.3.3):
用户输入例如 10,40(即 10 欧元和 40 美分的欧盟货币) - 然后我想获取该字符串输入并将其转换为例如,BigDecimal(或 Number 等),用于进一步的内部计算。
例如,如果用户输入 10.40(在美元的情况下相当于 10 美元 40 美分),则同样的操作应该起作用,然后采用该字符串输入并将其转换为 BigDecimal(或数字等...)。
我尝试了以下操作(基于 Michael Petrotta 的回答 here),失败并显示“ParseException: Unparsable Number 10,40):
// German input
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.GERMANY);
// User input
String value = "10,40";
Number valueParsed = formatter.parse(value);
我也尝试了“10,40 €”,但无济于事。 NumberFormat.parse 的目的还是我完全走错了路......?我真的想避免使用正则表达式来替换“10,40”中的逗号,因为该版本应该在国际上工作,我想要一些干净的,基于区域设置的方法。
编辑
我发现这个问题它解决了同样的问题 - 正确的答案使用与我相同的方法,所以我再次尝试(这次使用法国作为区域设置),但它仍然失败,这看起来很奇怪......引用问题下的评论建议使用ReplaceAll(",", ".") 这对我来说似乎是一个坏主意,因为某些货币使用逗号作为数千的分隔符,例如美元(如果我没有记错的话)。然而,我可以检查区域设置,但所有接缝都是阴暗的:(并且可能是错误的......
I have an EditText which should work as follows (Android 2.3.3):
The user enters for instance 10,40 (which is 10 Euro and 40 Cents in EU currency) - I want to then take that String input and convert it to a BigDecimal (or Number etc...) for instance, for further internal computation.
The same should work if the user enters 10.40 (which - in the case of US Dollar would amount to 10 Dollars and 40 Cents), then take that String input and convert it to a BigDecimal (or Number etc...) for instance.
I tried the following (based on Michael Petrotta's answer here), which fails with "ParseException: Unparsable Number 10,40):
// German input
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.GERMANY);
// User input
String value = "10,40";
Number valueParsed = formatter.parse(value);
I also tried "10,40 €" to no avail. So either I am misunderstanding the purpose of NumberFormat.parse or I'm on a wrong track all together...? I really want to avoid using Regular Expressions to replace the comma in "10,40" for the version should work internationally and I'd like some clean, Locale-based approach.
EDIT
I found this question which addresses the same issue - the correct answer uses the same approach that I do, so I tried again (using France as a Locale this time) but it fails nonetheless, which seems quite odd... The comment under the referenced question suggested using replaceAll(",", ".") which seems a bad idea to me, as some currencies use the comma as a divider for thousands, such as the US Dollar if I am not mistaken. I could however check the locale but that all seams shady :( and potentially erroneous ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来你并不真正需要货币格式。使用
NumberFormat.getNumberInstance()
代替:这会抛出
NumberFormatException
,但万一用户输入的格式不适合他的区域设置(例如“10,40”代表美国) ),但应该在正常情况下工作。It seems that you don't really need currency format. Use
NumberFormat.getNumberInstance()
instead:This will throw
NumberFormatException
though in case the user will enter the value in format inappropriate for his locale (e.g. "10,40" for US), but should work in regular case.您的字符串中缺少该区域设置的货币符号。
例如,对于德国货币符号是 €,
您的用户插入的数据应该是
String value = "10,40 €";
String value = "10,40"
下面的示例工作正常
You are missing currency notation for that locale in your string.
eg for Germany currency notation is €
your user inserted data should be
String value = "10,40 €";
instead ofString value = "10,40"
Following example works fine
不幸的是,我无法为您提供有关使用区域设置的更多见解,但可以回答您问题的其他部分:
如果你想要一些在国际上有效的东西,只需遵循以下步骤:
Unfortunately I cannot give you more insight on using Locale, but to answer the other part of your question:
If you want something that works internationally just follow this: