Loss of precision or error in Double conversion of large numbers in Java

when Double converts large numbers, there is a loss of precision or an error. For example, an error occurs when a number of 18 or more digits exceeds 100000000000000000. The problem can be solved by using BigDecimal or BigInteger, and the maximum value of Double is also much higher than this. Want to know why this is the case with Double?

    NumberFormat nf = NumberFormat.getInstance();
    nf.setGroupingUsed(false);
    System.out.println(nf.format(Double.valueOf("100000000000000000")));//100000000000000000
    System.out.println(nf.format(Double.valueOf("100000000000000001")));//100000000000000000
    System.out.println(nf.format(Double.parseDouble("100000000000000216")));//100000000000000224
    System.out.println(new BigDecimal("100000000000000216"));//100000000000000216
Apr.03,2021

double is


not all the numbers between the maximum and minimum values can be accurately expressed. It is obvious that a finite number of bytes cannot represent infinite precision (this set is even uncountable).


" effective java "48-if you need an exact answer, avoid using float and double


double is binary storage, something that appears to be an integer. Expressed by binary scientific counting, it becomes an infinite decimal

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea2cc0-1c04.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea2cc0-1c04.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?