0

I began read about Java programming these days, and I find something strange in simple code

double sum= 0.012 + 0.012+ 0.012;
        System.out.println("the value is" + sum);

the output is. 0.036000000000000004

but if I add 0.12 + 0.12 + 0.12 the result is 0.36 why?

Hayder Al-Amily
  • 329
  • 1
  • 4
  • 9
  • 2
    Google `Floating point error` – Arc676 Dec 28 '16 at 11:08
  • https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Amit Bhati Dec 28 '16 at 11:10
  • 2
    The *really* strange part: how many people using floating point numbers out there; without bothering to **understand** the concept they are using. Hint: when you are newbie, *anything* you stumble upon ... has been asked. Here. And elsewhere. A thousand times. And answered. – GhostCat Dec 28 '16 at 11:14
  • @GhostCat okay but I recently noticed that multiplication doesn't work in java :) – Jean-François Fabre Dec 28 '16 at 16:27

0 Answers0