Forked from djD-REK/Using exponential notation to prevent rounding errors.js
Created
May 9, 2021 19:56
-
-
Save marocas/cfb7b63c72407f0b2af7af2d56131f31 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| console.log(Number(Math.round(1.005 + "e2") + "e-2")) // 1.01 | |
| const roundAccurately = (number, decimalPlaces) => Number(Math.round(number + "e" + decimalPlaces) + "e-" + decimalPlaces) | |
| console.log(roundAccurately(1.005, 2)) // 1.01 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment