Created
March 19, 2015 03:58
-
-
Save salmonmoose/77120eafb467665f1f98 to your computer and use it in GitHub Desktop.
Round a number up to a significant number of digits.
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
function ceilToSignificant($number, $figures) { | |
$digits = ceil(log10($number < 0 ? -$number : $number)); | |
$power = $figures - (int) $digits; | |
$magnitude = pow(10, $power); | |
$shifted = ceil($number * $magnitude); | |
return $shifted / $magnitude; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment