1

i want to multiply 10.03 to 100 (exactly : 10.03 * 100).
I wrote this code below.
but the output is not what I expected.
i think the output should be 1003.0
but it prints 1002.9999999999999
why does it happen?
here's my code :

public class ComputeChange {

public static void main(String[] args) {
    double num = 10.03;
    System.out.println("number is : " + num);
    double multiply = num * 100;
    System.out.println("multiply to 100 : " + multiply);
}

}

  • Basically, when you write `10.03` in program source code, the number you get is not mathematically exactly equal to 10.03, but a very close approximation. See the answers to the duplicate question for more details. – Joni Aug 02 '20 at 14:13
  • it is possible if your convert your `multiply` into `int` or `long`. your case because of [Java uses IEEE-754 for representing floating point numbers](https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.3). [more info](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Hiep Le Aug 02 '20 at 14:42

0 Answers0