java - 解析 FR 货币时出现 UnparseableNumberException

标签 java numberformatexception nsnumberformatter

我不断收到 UnparseableNumberException在解析FR货币的同时。

我的代码:

Locale locale = new Locale("fr", "FR");
NumberFormat numberFormat = NumberFormat
                .getCurrencyInstance(locale);
try {
    Number number = numberFormat.parse("€ 314,00");
    System.out.println("sadsadsadsad"+number.doubleValue());
} catch (ParseException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

控制台输出:

java.text.ParseException: Unparseable number: "€ 314,00"
    at java.text.NumberFormat.parse(Unknown Source)
    at PS6.main(PS6.java:26)

最佳答案

在使用欧元作为货币的法国,价格的格式以货币符号结尾,前面有一个空格;不是一开始。因此,"€ 314,00" 是无效输入,因为 "€" 符号放错了位置,您应该输入 "314,00 €" 改为。

NumberFormat numberFormat = NumberFormat.getCurrencyInstance(Locale.FRANCE);
Number number = numberFormat.parse("314,00 €");
System.out.println(number);

此外,您可以使用内置的Locale.FRANCE,而不是创建新的语言环境。 .

请注意,并非所有使用欧元的国家都是如此;例如,爱尔兰将欧元符号放在数字前面,不带空格(并用点代替逗号作为小数点分隔符)。因此,对于语言环境 new Locale("en", "IE"),正确的格式为 "€314.00"...

关于java - 解析 FR 货币时出现 UnparseableNumberException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40506372/

相关文章:

java - 我可以根据实体或表使用不同的 Hibernate 批量大小吗?

java - BigDecimal 和 BigInteger 的限制是什么?

java - 什么是NumberFormatException,我该如何解决?

java - NumberFormatException错误

objective-c - NSNumberformatter 准科学记数法

ios - Swift IAP SK产品显示错误价格

java - Rename重构时如何防止IntelliJ IDEA删除未使用的包?

java - 查找数组所有可能组合的所有可能组合

iOS:删除 NSDecimalNumber 中的千位分隔符进行编辑

java - 这是 Java 中桥接模式的正确实现吗?