[!--quoteo(post=356334:date=Mar 19 2006, 05:22 AM:name=nawtwrong)--][div class=\'quotetop\']QUOTE(nawtwrong @ Mar 19 2006, 05:22 AM)

[/div][div class=\'quotemain\'][!--quotec--]

I have a list of pre determined numbers set as variables, I need a way of rounding these numbers to the nearest high or lower match in that list, to a number that a visitor will type in:

For example there will be a form with an input box for a number that the user types in. This will be submitted to a script where each of the pre-determined numbers in the list mentioned above will be stored as variables:

$number_1 = 1000 $number_2 = 2000 $number_3 = 3000 $number_4 = 4000 $number_5 = 10,000 etc

The user might type in a number like 2921, so the script would need to round this

**up** to 3000 ($number_3) the next highest in the list above.

I also want another script that would round the number

**down** so 9999 would be rounded down to 4000 ($number_4) the next lowest in the list above.

Is there a nice, simple way of doing this? I was thinking the numbers for the list could be stored in an array and maybe I could use the round() function, but I've no idea where to start.

Any advive or suggestions greatly appreciated.

[/quote]

something like this

<?php
function my_round($n,$a) {
sort($a);
$out=$a[0];
$p=abs($n-$out);
for ($i=1;$i<count($a);$i++) {
if (abs($a[$i]-$n)<=$p) {
$out=$a[$i];
$p=abs($n-$out);
}
}
return $out;
}
function my_round_down($n,$a){
sort($a);
$out=$a[0];
for ($i=0;$i<count($a);$i++) {
if ($a[$i]<$n){
$out=$a[$i];
}
}
return $out;
}
$pre=array(1000,2000,3000,4000,10000);
$number=9999;
echo "for number ".$number."<br />\n";
echo "my_round is: ".my_round($number,$pre)."<br />\n";
echo "my_round_down is: ".my_round_down($number,$pre)."\n";
?>