简体   繁体   English

在法国的机器上执行时java.lang.NumberFormatException

[英]java.lang.NumberFormatException while executing in France machine

In below code while parsing the value sometimes i am facing NumberFormat Exception in France machine. 在下面的代码中,在解析值时,有时我在法国机器中遇到NumberFormat异常。

double txPower;
DecimalFormat df = new DecimalFormat("##.##");

txPower = txPower + getDeltaP();
log.info("txpower value is -- "+txPower);
txPower = Double.parseDouble(df.format(txPower));


protected double getDeltaP() 
{
    return isNewChannelAddition ? apaConfig.deltaPadd : apaConfig.deltaPtune;
}

logs: 日志:

txpower value is -- -7.9
java.lang.NumberFormatException: For input string: "-7,9"

我建议对您的语言环境使用配置为默认值的十进制分隔符。

new DecimalFormatSymbols(Locale.getDefault(Locale.Category.FORMA‌​T)).getDecimalSepara‌​tor();

You have to ways to solve your problem : 您必须设法解决您的问题:

One, you can use replace(",", ".") like this : 一,您可以像这样使用replace(",", ".")

txPower = Double.parseDouble(df.format(txPower).replace(",", "."));

Two, you can use the local for the DecimalFormat : 二,您可以使用本地的DecimalFormat

DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
df.applyLocalizedPattern("##.##");
txPower = txPower + getDeltaP();
txPower = Double.parseDouble(df.format(txPower));

您也可以调用类似String.format("%.2f", -7.9)

Double.parseDouble doesn't support locale-specific decimal points (see the doc here ). Double.parseDouble不支持特定于语言环境的小数点(请参阅此处的文档)。 Try using your DecimalFormat to parse instead: 尝试使用DecimalFormat解析:

txPower = df.parse(df.format(txPower)).doubleValue();

Having said that, I must ask what you are hoping to gain by to turning the double value txPower into a String , parsing the string into a double and putting the result back into txPower ? 话虽如此,我必须问一下,将doubletxPower转换为String ,将字符串解析为double并将结果放回txPower ,您希望获得什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM