Round Number to Nearest


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.