-4

This code returns: 0.04999999999999999

var number = parseFloat(0.15);
console.log(number % 0.05);

Why?

Here is a plunk: http://plnkr.co/edit/fYa4ZpvNj3xTvgNHx0pj?p=preview

Fortuna
  • 611
  • 6
  • 18
  • 1
    `parseFloat` parses a string to a number. What's the point of using it on a number? – Oriol May 02 '16 at 23:10
  • duplicate of http://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript – Stephen Thomas May 02 '16 at 23:11
  • In my case "0.15" comes from user input which is a string and I don't know if the user actually entered a number or just some random string. – Fortuna May 02 '16 at 23:12

1 Answers1

0

Floating point math is actually only as good as the bit width representation. For instance, 0.1 is actually 0.100000010000001.

b degnan
  • 672
  • 1
  • 14
  • 31