You are here: Re: [PHP] Round to the nearest X - SOLVED « PHP « IT news, forums, messages
Re: [PHP] Round to the nearest X - SOLVED

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация