java.lang.NumberFormatException和SystemDecimalSeparator
有这个错误java.lang.NumberFormatException和SystemDecimalSeparator,java,Java,有这个错误 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
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)
读完这篇文章后,我试着
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已经是“,”您正在调用的
Double.parseDouble
,它总是使用一个点作为十进制分隔符-它根本不使用默认的区域设置
有文件证明其行为类似,其文件包括:
要解释浮点值的本地化字符串表示形式,请使用NumberFormat
的子类
您正在调用
Double.parseDouble
,它总是使用一个点作为十进制分隔符-它根本不使用默认区域设置
有文件证明其行为类似,其文件包括:
要解释浮点值的本地化字符串表示形式,请使用NumberFormat
的子类
这应该起作用:
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
这应该起作用:
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
可能值得一看下面的帖子请发布数字的完整堆栈跟踪FormatException可能值得一看下面的帖子请发布数字的完整堆栈跟踪NumberFormatException@NassimMOUALEK:这是记录在案的行为-你为什么希望我错了?正如我所说的,如果您想解析本地化表示,请使用
NumberFormat
…考虑到还有其他方法可以解析,非常感谢mutch@NassimMOUALEK:这是记录在案的行为-你为什么希望我错了?正如我所说的,如果您想解析本地化表示,请使用NumberFormat
…考虑到还有其他方法可以解析,非常感谢