Java: Convert Exponential form to Decimal number format in Java

While working with Doubles and Long numbers in Java you will see that most of the value are displayed in Exponential form. For example : In following we are multiplying 2.35 with 10000 and the result is printed.
//Division example Double a = 2.85d / 10000; System.out.println("1) " + a.doubleValue()); //Multiplication example a = 2.85d * 100000000; System.out.println("2) " + a.doubleValue());
Code language: Java (java)
Result:
1)  2.85E-4
2)  2.85E8
Thus you can see the result is printed in exponential format. Now you may want to display the result in pure decimal format like: 0.000285 or 285000000. You can do this simply by using class java.math.BigDecimal. In following example we are using BigDecimal.valueOf() to convert the Double value to BigDecimal and than .toPlainString() to convert it into plain decimal string.
import java.math.BigDecimal; //.. //.. //Division example Double a = 2.85d / 10000; System.out.println("1) " + BigDecimal.valueOf(a).toPlainString()); //Multiplication example a = 2.85d * 100000000; System.out.println("2) " + BigDecimal.valueOf(a).toPlainString());
Code language: Java (java)
Result:
1)  0.000285
2)  285000000
The only disadvantage of the above method is that it generates lonnnnggg strings of number. You may want to restrict the value and round off the number to 5 or 6 decimal point. For this you can use java.text.DecimalFormat class. In following example we are rounding off the number to 4 decimal point and printing the output.
import java.text.DecimalFormat; //.. //.. Double a = 2.85d / 10000; DecimalFormat formatter = new DecimalFormat("0.0000"); System.out.println(formatter .format(a));
Code language: Java (java)
Result:
0.0003
Happy converting :)
Get our Articles via Email. Enter your email address.

You may also like...

13 Comments

  1. Azeem says:

    Thanks…it helped me

  2. Jyothsna says:

    Thanks dude.. it helped me too..

  3. Ambika says:

    I dont want the result to be string, i need it as a number. How do i get this?
    If i convert this string to number it again becomes exponential. Any help on this would be appreciated.

    • saneshsabu says:

      use BigDecimal.valueOf(a) . Its of BigDecimal Type.

      • tushar banne says:

        It still fails when the number is 0.0000003.

  4. omer yousaf says:

    thanks. i had made my own formula/arithmetic expression parser which was facing problems due to an inconsistent way java presents its results in double format; mostly the numbers remained in decimal form, but too small and too large results were returned in the exponential format. just using the BigDecimal whenever an a computation returned a result straightened out the bug for me.

  5. Sree says:

    Thanks a lot for the solution.
    Saved my time. :)

  6. Harikumar says:

    Thanks for your example. It really helped a lot. Keep posting.

  7. murali says:

    hi.. nice one… but if i have a value like 5.0 its a double value, i need to store again into another double variable in the format like 5.000, how it can be possible

    • Sireesh Yarlagadda says:

      DecimalFormat df=new DecimalFormat(“#.000”)
      df.parse(yournumber)

  8. Khoa says:

    Thanks a lot

  9. Khoa says:

    Thanks a lot

  10. lan says:

    doesnt work for the number -7.728382078632706e+19
    please help

Leave a Reply

Your email address will not be published. Required fields are marked *