0

If I perform the following instructions in NodeJS:

> 4.676 - 0.001
4.675
> 4.678 - 0.001
4.677
> 4.677 - 0.001
4.675999999999999

Why is not it accurate like the others? Version Node JS is 8.11.1

1 Answers1

0

This is a rounding issue found in most computer science.

Here are 2 articles:

  1. https://floating-point-gui.de/errors/rounding/
  2. https://softwareengineering.stackexchange.com/questions/101163/what-causes-floating-point-rounding-errors

For now, use Number((4.677 - 0.001).toFixed(3)); to the precision you need.

Matt Kuhns
  • 1,328
  • 1
  • 13
  • 26