0

Why is Math.floor(1.9) considered as 1 but Math.floor(1.9999999999999999) as 2?

console.log(Math.floor(1.9));// result ==> 1;
console.log(Math.floor(1.9999999999999999));// result ==> 2
Biswas Khayargoli
  • 976
  • 1
  • 11
  • 29
  • 5
    Try this: `console.log(1.9999999999999999)` and see what you get. – Jesper Apr 23 '18 at 12:36
  • @Jesper I get 2 – Biswas Khayargoli Apr 23 '18 at 12:39
  • 4
    Indeed. Because `1.9999999999999999` is equal to `2`. Floating-point numbers are not infinitely precise, and JavaScript interprets `1.9999999999999999` as equal to `2`. So when you do `Math.floor(1.9999999999999999)` it is the same as `Math.floor(2)` which is `2`. – Jesper Apr 23 '18 at 12:41

0 Answers0