Re: Round to the nearest X - SOLVED

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Thank you for everyone's suggestions.  Using them
(and a few other things I've found elsewhere), I've
come up with the following function:

function roundToNearest( $number, $toNearest = 5 ) {

  $retval = 0;

  $mod = $number % $toNearest;

  if( $mod >= 0 ) {
    $retval = ( $mod > ( $toNearest / 2 )) ? $number + ( $toNearest - $mod )
: $number - $mod;

  } else {
    $retval = ( $mod > ( -$toNearest / 2 )) ? $number - $mod : $number +
( -$toNearest - $mod );

  }
  return $retval;

} // end function round();

that works with positive and negative numbers and seems
to work with every test I threw at it.

Thanks again for everyone's help!

thnx,
Chris

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux