I\'m having trouble with (what I suspect is) a rounding error.
I have a string, 0.686357E-01
, which I\'m trying to convert to a double. I\'ve been able to s
0.0686357 is not exactly representable as a double-precision value.
Two solutions:
BigDecimal
.Floating point numbers do not have perfect precision. If that is an issue, use BigDecimal:
String string = "0.686357E-01";
BigDecimal number = new BigDecimal(string);
System.out.println(number);
Double
will print always like that, but the value will remain correct. You'll need to format the output to get the correct value. See DecimalFormat class.
You don't need to split it, Double.parseDouble
can handle those kinds of numbers just fine.
double number = Double.parseDouble("0.686357E-01");
See? It works!