1

I'm facing the problem with NumberFormatException. For input string for the French language. When I choose English / Spanish everything works fine! But when I choose French immediately it crashes and shows 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)

I'm facing problem with power3 value. Its value is 512000.0 for English / Spanish, but 512000,00 for French.

Azeem
  • 11,148
  • 4
  • 27
  • 40
Unknown
  • 79
  • 8
  • I don't recommend using string to do this kind of truncation: `BigDecimal` should be used instead, e.g.: `int places = 3; BigDecimal val = BigDecimal.valueOf(1/3d); BigDecimal out = val.round(new MathContext(val.precision() + places - val.scale(), RoundingMode.HALF_EVEN)); double result = out.doubleValue();` – fabian Aug 29 '18 at 10:03

1 Answers1

0

Try with replace():

 Double.parseDouble(String.format("%.2f", Math.pow(temp, 3)).replace(',', '.'))
fabian
  • 80,457
  • 12
  • 86
  • 114
Gokul Nath KP
  • 15,485
  • 24
  • 88
  • 126
  • if i have **n** number of places what i have to do..? I need to replace all the places? I don't think that's a good practice. – Unknown Aug 29 '18 at 09:21