我不断收到 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/