当我尝试解析“ 10,10€”时,NumberFormat引发ParseException.
@Test
public void get_currency_from_text() throws Exception {
String moneyAsString = "10,10 €";
//Do not use double for monetary values
Double moneyAsDouble = 10.10;
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.GERMANY);
Double parsedMoney = formatter.parse(moneyAsString).doubleValue();
assertEquals(moneyAsDouble, parsedMoney);
}
这是我运行测试时抛出的内容
java.text.ParseException: Unparseable number: "10,10 €"
无论何时我这样做都可以正常工作:
@Test
public void get_currency_from_text() throws Exception {
Double moneyAsDouble = 10.10;
NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.GERMANY);
String moneyAsString = formatter.format(moneyAsDouble);
Double parsedMoney = formatter.parse(moneyAsString).doubleValue();
System.out.println(moneyAsString);
//10,10?€
assertEquals(moneyAsDouble, parsedMoney);
}
我怀疑它与空间有关,但是我仍然不知道如何解决它,有什么想法吗?