Created
April 8, 2011 19:29
-
-
Save zacharydanger/910551 to your computer and use it in GitHub Desktop.
How to properly round numbers in PHP
This file contains hidden or 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
<?php | |
/** | |
* works around PHP's shitty floating point math to properly round numbers | |
* | |
* USE THIS EVERYWHERE BECAUSE FLOATS FUCKING SUCK | |
*/ | |
function super_round($number, $precision = 0) { | |
$power = bcpow(10, $precision); | |
$rounded = round(bcmul($number, $power, 1), 0); | |
return bcdiv($rounded, $power, $precision); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment