Created
January 28, 2020 03:45
-
-
Save npearce/86470b60bcb31fc6eb4cbf53eceb7b24 to your computer and use it in GitHub Desktop.
Javascript Floating Point Arithmetic
This file contains 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
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