Having this error
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)
after reading decimal-separator-in-numberformat this, i try
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);
}
}
finally the SystemDecimalSeparator is already the ',' so why this exception ?
Its probably expecting 20.00 but how to get and fix this separtor ?
Indeed i test and its expectiong "20.00" instead and the default SystemDecimalSeparator is already the ','
You're calling Double.parseDouble
, which always uses a dot as the decimal separator - it doesn't use the default locale at all.
Double.parseDouble
is documented to behave like Double.valueOf(String)
, which has documentation including:
To interpret localized string representations of a floating-point value, use subclasses of
NumberFormat
.
See more on this question at Stackoverflow