1

My understanding is the maximum digits to safely represent a number as double in Java is 2^53-1, or 9007199254740991. And that is regardless of decimals because that's represented by exponent bits. Consider the following:

    double dblprice = 9007.199254740991;
    System.out.println(dblprice);
    dblprice = 9007199.254740991;
    System.out.println(dblprice);

The output is

9007.199254740992
9007199.25474099

Which I don't understand why for first case the last 1 is replaced by 2 and for the 2nd case, the last digit is dropped. Thanks a lot!

Theboy
  • 353
  • 1
  • 2
  • 8
  • The exponent deals with binary fractions, not decimal fractions. The result you have is the closest decimal representation of the binary fraction that was the closest to your input number. (Also, `MAX_SAFE_INTEGER` is a JavaScript concept, and not relevant here.) – Amadan Aug 30 '19 at 08:16
  • Use `BigDecimal` if you need arithmetic accuracy. – Thomas Timbul Aug 30 '19 at 10:04
  • Okay, I see, java radix is 2, not 10, which is where my confusion was. Okay thanks. I think I get it – Theboy Aug 30 '19 at 20:33

0 Answers0