NumberFormatException用于法语的输入字符串

时间:2018-08-29 06:35:02

标签: java android numberformatexception

我遇到NumberFormatException的问题。用于法语的输入字符串。当我选择英语/西班牙语时,一切正常!但是当我立即选择法语时,它崩溃并显示NFException

public void TemperatureCurrentLevel(DosageCalculatorInputDto dosagecalculatorinput, DosageCalculatorResponseDto dosageCalculatorResponseDto) {
    double temp = dosagecalculatorinput.getTemperatureNow();
    Log.e("temp value", ""+Double.parseDouble(String.format("%.2f", Math.pow(temp, 3))));
    double power3 = Double.parseDouble(String.format("%.2f", Math.pow(temp, 3)));
    double power2 = Double.parseDouble(String.format("%.2f", Math.pow(temp, 2)));
    Double result = (-0.0000005 * power3 + 0.00006 * power2 + 0.0117 * temp - 0.4116);
    Log.i("teja1", "temperature factor current " + Double.parseDouble(String.format("%.3f", result)));

    dosageCalculatorResponseDto.setTemparatureFactoreCurrent( result);
}

Logcat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elitecrest.orenda/com.elitecrest.orenda.MainActivity}: java.lang.NumberFormatException: For input string: "512000,00"
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2721)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2782)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1521)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6228)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
 Caused by: java.lang.NumberFormatException: For input string: "512000,00"
    at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306)
    at java.lang.Double.parseDouble(Double.java:547)
    at com.elitecrest.orenda.utils.FormulationClass.TemperatureCurrentLevel(FormulationClass.java:64)

我面临着power3值的问题。对于英语/西班牙语,其值为 512000.0 ,对于法语,其值为 512000,00

1 个答案:

答案 0 :(得分:0)

尝试使用replace()

 Double.parseDouble(String.format("%.2f", Math.pow(temp, 3)).replace(',', '.'))