0

Console modulo result

When I doing an exercise on Free Code Camp, I find out this error - the result of 96.74%20 was 16.739999999999995, not 16.74. What's wrong with it and how can I fix this error?

console.log(96.74%20); // 16.739999999999995
Huy Ngo
  • 125
  • 9
  • You can use: `console.log((96.74%20).toFixed(2));` – MHS Jul 08 '16 at 10:49
  • 1
    This is the effect of floating point rounding error. Read this: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Stephen C Jul 08 '16 at 10:50
  • Any Number object in javascript has a method called "toFixed" that will return the same number rounded to the Nth decimal round: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed . In your case, using .toFixed(2) is just enough. In any case, such an error is related to **floating points rounding errors**, you may check some in google if you're interested to know what it is and why this happens. – briosheje Jul 08 '16 at 10:51

1 Answers1

0

Try this:

console.log((96.74%20).toFixed(2)); // 16.739999999999995
MHS
  • 881
  • 1
  • 13
  • 30