-12

When converting 0.0001 from string to double using Double.parseDouble(stringValue) returns 1.0E-4. How to get a 4 decimal placed double value as 0.0001.

Arundas K V
  • 801
  • 2
  • 14
  • 28
  • String.format("%.4f",secondNumber); – ADM Dec 22 '17 at 09:32
  • 0.0001 is a string and i want the output to a double. exactly same as 0.0001, not to a string. – Arundas K V Dec 22 '17 at 10:13
  • 1
    @AndEngine But [1.0E-4 **is exactly the same** as 0.0001](https://www.wolframalpha.com/input/?i=1.0e-4) in terms of how the data is stored as a double. You're just getting a different representation when you're converting back to a string when you're logging the output. – Michael Dodd Dec 22 '17 at 10:30

1 Answers1

2

You could you use DecimalFormat to define how much positions after coma should be shown.

String stringValue= 0.0001;
DecimalFormat df = new DecimalFormat("##0.###");
df.format(stringValue);

1.0E-4 means something like 1.0 * 10^-4 which is the same as 0.0001

More about DecimalFormat look here https://developer.android.com/reference/java/text/DecimalFormat.html

Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
Franz
  • 358
  • 6
  • 18
  • i want the output to a double variable – Arundas K V Dec 22 '17 at 10:08
  • That's the standard way Java stores Double Values, doesn't know any way to change this. It's not important how double is stored intern, you can just convert it when printing it on screen. The user will not mind. – Franz Dec 30 '17 at 16:56