0

I'm trying to convert my following String to double value

String str = "26315000000253009";
System.out.println("Parsing double value i got : "+Double.parseDouble(str));

Result

Parsing double value i got : 2.6315000000253008E16

But the expected one is 2.6315000000253009E16

Why does this inconsistency prevails?

  • This question must have been asked before, but more importantly why does this matter? – Tim Biegeleisen May 02 '17 at 04:51
  • see this answer http://stackoverflow.com/questions/179427/how-to-resolve-a-java-rounding-double-issue – Leon May 02 '17 at 04:53
  • @TimBiegeleisen : problem comes from json. When I add the string value `26315000000253009` and obtain it as json.getLong() - json does the following conversion `Double.parseDouble("26315000000253009")` - when it is not a number. So, I'm getting inconsistent result there. But I'm curious to know how Double.parseDouble() gives inconsistent result.. –  May 02 '17 at 04:54
  • Try System.out.printf("Parsing double: %f\n", Double.parseDouble(str)); But be aware that it will give you 26315000000253008.00000. – sAm May 02 '17 at 04:57
  • Double cannot exactly represent all numbers. For `26315000000253009` the nearest representable numbers are `26315000000253008` and `26315000000253012`. – Thomas Kläger May 02 '17 at 04:57
  • @ThomasKläger : What's the reason behind such limitation.. Just curious to know what happens internally during `String` to `Double` conversion.. And how much length does `Double` can hold the value - so that it can produce accurate result? –  May 02 '17 at 04:58
  • @sAm : It produces some other value like `2.6315000000253004E16` –  May 02 '17 at 05:00
  • 2
    @rajasubasubramanian33 You have to keep in mind that these numbers are stored in binary and the binary representation can not always accurately represent the number you desire. – Michael Markidis May 02 '17 at 05:04
  • @ThomasKläger : How could you say that the nearest possible numbers are `26315000000253008` and `26315000000253012`? –  May 02 '17 at 05:13
  • Doubles only have about 15.9 decimal digits of integral precision. Your number has 17. Your expectations are baseless. – user207421 May 02 '17 at 05:21
  • 2
    The reason behind the limitation is that double use 64 bits to store very small (around 10^-324) and very large (around 10^308) numbers. Therefore it cannot use all 64 bits to store the digits of the number. For details see https://en.wikipedia.org/wiki/Floating-point_arithmetic#IEEE_754:_floating_point_in_modern_computers. I found the nearest numbers by doing `double n = Double.parseDouble("26315000000253009"); System.out.println(n); System.out.println(n+1); System.out.println(n+2); System.out.println(n+4);` – Thomas Kläger May 02 '17 at 05:22
  • @EJP : Does 15 represents `manitssa` part and `9` represents `exponential` part? –  May 02 '17 at 06:17
  • 1
    @rajasubasubramanian33 No, 15.9 is the approximate number of decimal digits that would have the same number of values as 53 binary digits. – Patricia Shanahan May 02 '17 at 07:36
  • @PatriciaShanahan : I couldn't get you what does `15.9` mean? Please explain me bit more.. –  May 02 '17 at 07:41
  • @ThomasKläger You can also use [Math.nextUp](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#nextUp-double-). – Patricia Shanahan May 02 '17 at 07:42
  • 1
    @rajasubasubramanian33 In practice, in this situation, it means "slightly less than 16". It is calculated as 53*log_10(2). – Patricia Shanahan May 02 '17 at 07:45
  • Okay @PatriciaShanahan. Does 16 represents the number of decimal digits? –  May 02 '17 at 08:05
  • @rajasubasubramanian33 No, ***15.9*** represents the number of decimal digits, as I originally wrote. – user207421 May 02 '17 at 09:48
  • @PatriciaShanahan thanks, I didn't know this method. I suspected that something similar should exist but did not take the time to search for it. – Thomas Kläger May 02 '17 at 10:20
  • @EJP : how could you say that the number of decimal digits which it could represent is **15.9** - Just curious to know.. –  May 02 '17 at 12:43
  • 1
    @rajasubasubramanian33 One way of looking at it is that the smallest natural number that cannot be represented exactly is 9,007,199,254,740,993. All the natural numbers that take 1 through 15 digits, and most of the 16 digit ones can be represented exactly. – Patricia Shanahan May 02 '17 at 13:19
  • 1
    @rajasubasubramanian33 The essential point here is that whether you think of it as 15.9 or 16, it is still less than 17. – user207421 May 02 '17 at 21:10
  • Yeah !! @EJP :) –  May 06 '17 at 09:55

0 Answers0