SystemDecimalSeparator

SystemDecimalSeparator

有这个错误

java.lang.NumberFormatException: For input string: "20,00"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at com.agitech.autofaces.el.FormatUtil.parseDouble(FormatUtil.java:122)
    at com.agitech.erp.converter.DoubleConverter.getAsObject(DoubleConverter.java:27)


阅读decimal-separator-in-numberformat这之后,我尝试

public class FormatUtil {
    public static char ApplicationDecimalSeparator = ',';
    public static char SystemDecimalSeparator;
    static {
        DecimalFormatSymbols symbols= DecimalFormatSymbols.getInstance();
        SystemDecimalSeparator = symbols.getDecimalSeparator();
        symbols.setDecimalSeparator(ApplicationDecimalSeparator);
    }
    public final static Double parseDouble(String d){
        if (d==null)
            return 0.0;
        return Double.parseDouble( fixDecimalSeparator(d) );
    }
    public final static String fixDecimalSeparator(String d){
        if (SystemDecimalSeparator==ApplicationDecimalSeparator)
            return d;
        return d.replaceAll( ""+SystemDecimalSeparator, ""+ApplicationDecimalSeparator);
    }
}


最后SystemDecimalSeparator已经是'',那么为什么会出现此异常?

它可能期望20.00,但如何获取并修复此分离器?

实际上,我测试了它的期望值“ 20.00”,默认的SystemDecimalSeparator已经是','

最佳答案

这应该工作:

NumberFormat f = NumberFormat.getInstance(); // Gets a NumberFormat with the default locale, you can specify a Locale as first parameter (like Locale.FRENCH)
double myNumber = f.parse("20,0").doubleValue(); // myNumber now contains 20

09-25 21:01