Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save djD-REK/068cba3d430cf7abfddfd32a5d7903c3 to your computer and use it in GitHub Desktop.
Save djD-REK/068cba3d430cf7abfddfd32a5d7903c3 to your computer and use it in GitHub Desktop.
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
@cccabdalla
Copy link

@hamidgasmi Nope! JavaScript's built-in math isn't great if you actually need precision or are working with exponential notation. I'd recommend using a library. MathJS is a good place to start. https://mathjs.org/

great, thanks.

very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment