Last active
August 29, 2015 14:01
-
-
Save connor4312/7f541daf82d7b6b12314 to your computer and use it in GitHub Desktop.
Javascript Semi-Signficant Figures
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
/** | |
Handy function to get (kind of) significant figures from Javascript numbers. | |
Unlike built-in Javascript methods, this works for all numbers, not just decimals. | |
Example: sigFigs(12345, 2) -> 12000 | |
sigFigs(0.022, 1) -> 0.02 | |
**/ | |
function sigFigs(num, figures) { | |
var delta = Math.pow(10, Math.ceil(Math.log(num) / Math.log(10)) - figures); | |
return Math.round(num / delta) * delta; | |
} |
I don't know why you would have to calculate something like this manually, but no problem! :D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Connor, one less thing I have to calculate on paper in my math class next quarter!