1

Currently learning Java, can anyone explain why the following code yields this output?

System.out.println(330*1.1);

363.00000000000006

When it should simply equal 363. Why is there the trailing decimal?

Alternatively, the following code correctly outputs 363.0 yet is mathematically equivalent.

double check = 330; 
check = check + check*0.1; 
System.out.println(check);
  • 1
    Because floating-point is an approximation. In particular, there's no exact way to represent 1.1 in IEEE double precision. – Hot Licks Mar 08 '15 at 01:34

0 Answers0