可能是重复问题:
安全且支持本地化的解析数字
我该如何以本地化方式验证包含小数的字符串?NumberFormat.parse允许过多,而Double.parseDouble仅适用于英语环境。以下是我的尝试:
public static void main(String[] args) throws ParseException {
Locale.setDefault(Locale.GERMAN);
NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.getDefault());
Number parsed = numberFormat.parse("4,5.6dfhf");
System.out.println("parsed = " + parsed); // prints 4.5 instead of throwing ParseException
double v = Double.parseDouble("3,3"); // throws NumberFormatException, although correct
}