Posted by Chris Boget on 04/29/05 16:20
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
[Back to original message]
|