Skip to content

Instantly share code, notes, and snippets.

@npearce
Created January 28, 2020 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save npearce/86470b60bcb31fc6eb4cbf53eceb7b24 to your computer and use it in GitHub Desktop.
Save npearce/86470b60bcb31fc6eb4cbf53eceb7b24 to your computer and use it in GitHub Desktop.
Javascript Floating Point Arithmetic
233.66 + 157.55
// returns: 391.21000000000004
// Because: https://stackoverflow.com/questions/588004/is-floating-point-math-broken/27030789#27030789
// As toFixed() converts to a string, use Math.round()
// 2 decimal places:
Math.round( (233.66 + 157.55)) * 1e2 ) / 1e2
// 3 decimal places:
Math.round( (233.66 + 157.55)) * 1e3 ) / 1e3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment