Skip to content

Instantly share code, notes, and snippets.

@zacharydanger
Created April 8, 2011 19:29
Show Gist options
  • Save zacharydanger/910551 to your computer and use it in GitHub Desktop.
Save zacharydanger/910551 to your computer and use it in GitHub Desktop.
How to properly round numbers in PHP
<?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