Posted By

jodm on 04/07/10


Tagged

number round nearest


Versions (?)

Round Number to Nearest


 / Published in: PHP
 

  1. function roundnum ($num, $nearest)
  2. {
  3. $ret = 0;
  4. $mod = $num % $nearest;
  5.  
  6. if ($mod >= 0)
  7. $ret = ( $mod > ( $nearest / 2)) ? $num + ( $nearest - $mod) : $num - $mod;
  8. else
  9. $ret = ( $mod > (-$nearest / 2)) ? $num - $mod : $num + ( -$nearest - $mod);
  10. return $ret;
  11. }

Report this snippet  

You need to login to post a comment.