我正在尝试解析以下字符串:“59000,00”
在巴西,逗号用于表示小数位,小数点用作分隔千位的符号。
我正在尝试做的事情:
final String price = "59000,00";
// LocaleUtils.getLocale returns new Locale("pt", "BR")
final NumberFormat numberFormat = NumberFormat.getCurrencyInstance(LocaleUtils.getLocale());
try {
final double d = numberFormat.parse(price).doubleValue();
// do stuff
} catch (ParseException e) {
// do stuff
}
但是,我得到了 ParseException。为什么会这样?
java.text.ParseException:无法解析的数字:“59000,00”(在偏移量 8 处)
最佳答案
您可以使用 GERMANY Locale,因为它使用逗号作为小数点分隔符,如 here in the documentation 所述.像这样:
NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY);
double df = nf.parse(price).doubleValue();
关于java - 解析 BRL 货币值字符串以加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49517897/