0

I'm new to Java and learning about the % operator. However I have a problem:

System.out.println(510%5); //this produces a correct output of 0

System.out.println((5.1*100)%(0.05*100)); //but this produces 4.9999

Technically they're the same numbers, but why are the outputs different?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • A question that [gets asked a lot](https://www.google.com/search?q=site%3Astackoverflow.com+java&oq=s&ie=UTF-8#q=site:stackoverflow.com+java+floating+point+precision) on this and on many other sites. – Hovercraft Full Of Eels Dec 05 '16 at 03:04
  • It is a remainder operator, not a modulus operator. – user207421 Dec 05 '16 at 03:26
  • Try `System.out.println(5.1*100)` and `System.out.println(0.05*100)`. The second one happens to round to the correct value, but the first one does not. – ajb Dec 05 '16 at 03:59

0 Answers0