0

I have a below program which is reading and displaying decimal numbers.

import java.math.*;
import java.text.DecimalFormat;
import java.text.ParseException;

public class Main {
    public static void main (String[] args) {
      String valStr = "-5.36870912E+9";
      Object val = null;
      DecimalFormat mDecimalFormat = new DecimalFormat("#.#");
      mDecimalFormat.setParseBigDecimal(true);
      mDecimalFormat.setMaximumFractionDigits(38);
      try {
        val = new BigDecimal(mDecimalFormat.parse(valStr).toString());
        System.out.println("valStr = " + valStr + " val = " + val + " mDecimalFormat.parse(valStr) " + mDecimalFormat.parse(valStr));
      }
      catch (ParseException ee) {
          System.out.println("Caught Exception");
      }
  }
}

It generates a below output.

valStr = -5.36870912E+9 val = -5.36870912 mDecimalFormat.parse(valStr) -5.36870912

Here the issue is, parse ignores the exponential format.

I tried to use below code with BigDecimal and it works fine.

BigDecimal temp = new BigDecimal(valStr);
val = new BigDecimal(mDecimalFormat.parse(temp.toPlainString()).toString());
Output with above changes are 
valStr = -5.36870912E+9 val = -5368709120 mDecimalFormat.parse(valStr) -5.36870912

But the problem is, if I try to use inputs from different locale, For example, French as shown below.

String valStr = "-5,36870912E+9";
Ending up with below exception.
Exception in thread "main" java.lang.NumberFormatException: Character , is neither a decimal digit number, decimal point, nor "e" notation exponential mark.
    at java.base/java.math.BigDecimal.<init>(BigDecimal.java:522)
    at java.base/java.math.BigDecimal.<init>(BigDecimal.java:405)
    at java.base/java.math.BigDecimal.<init>(BigDecimal.java:838)
    at Main.main(Main.java:14)

I have referred various pages, most of the suggestions were to use BigDecimal, Double.

Is there any way i could convert the string "-5,36870912E+9" to "-5368709120" before calling parse method?

10
  • 1
    Best way to convert Locale specific String to BigDecimal Commented Sep 4, 2024 at 11:52
  • 3
    "Is there any way ..."--> DecimalFormat.getNumberInstance(appropriateLocaleHere).parse(valStr.replace("E+","E")) Commented Sep 4, 2024 at 12:22
  • 4
    @RandomGuy It supports E- just fine. So E for positive exponents and E- for negative exponents. Commented Sep 4, 2024 at 13:33
  • 1
    It's a pity DecimalFormat is so inflexible. Since Double.toString() outputs "...E+...", it would have been nice if it parsed it. Also, DecimalFormat doesn't work with Indian locales. Commented Sep 4, 2024 at 13:57
  • 3
    @k314159 DecimalFormat and NumberFormat have been around since Java 1.1 Perhaps it's long overdue for an update. Commented Sep 4, 2024 at 14:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.