Jump to content


Photo

Solution needed for rounding number in php


  • Please log in to reply
7 replies to this topic

#1 nawtwrong

nawtwrong
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 19 March 2006 - 04:22 AM

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.



#2 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 19 March 2006 - 05:54 AM

Not a definite solution for you, but maybe something to start you off with:


function round_to($number,$closest)
{
return round($number/$closest)*$closest;
}

echo round_to(4501,1000); //rounds to 5000


#3 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 19 March 2006 - 03:28 PM

[!--quoteo(post=356334:date=Mar 19 2006, 05:22 AM:name=nawtwrong)--][div class=\'quotetop\']QUOTE(nawtwrong @ Mar 19 2006, 05:22 AM) View Post[/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";
?>


#4 nawtwrong

nawtwrong
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 19 March 2006 - 10:36 PM

Thanks for replying, some very useful info there. I'll let you know how I get on.

Thanks again!!

#5 nawtwrong

nawtwrong
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 March 2006 - 04:36 AM

I've got the code working how I need it to, but there's just one problem:

"Fatal error: Cannot redeclare my_round_down()"

I'm excecuting this code using
include("../code.php");
inside a while loop which is what's causing the error. However, I've tried using require_once, this clears the error message, but causes the function to do the same thing to all the values that need rounding in the while loop.

Is there a way round this? The my_round_down() function (exactly as above) needs to run for each number in a while loop and round each number individually.

Thanks


#6 nawtwrong

nawtwrong
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 March 2006 - 06:02 AM

Right, sorted it. I put some code in to work out whether or not rounded down or rounded up was needed and called this right at the beginning og the parent script: This seems to have done the trick.

Thanks again for your help!

#7 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 20 March 2006 - 06:08 AM

[!--quoteo(post=356577:date=Mar 20 2006, 05:36 AM:name=nawtwrong)--][div class=\'quotetop\']QUOTE(nawtwrong @ Mar 20 2006, 05:36 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I've got the code working how I need it to, but there's just one problem:

"Fatal error: Cannot redeclare my_round_down()"

I'm excecuting this code using
include("../code.php");
inside a while loop which is what's causing the error. However, I've tried using require_once, this clears the error message, but causes the function to do the same thing to all the values that need rounding in the while loop.

Is there a way round this? The my_round_down() function (exactly as above) needs to run for each number in a while loop and round each number individually.

Thanks
[/quote]
move declaration of function(s) oueside loop. something like this
//start of file
include('../code.php');

// some code

$pre=Array(1000,2000 and so on); // if this array is constant you can defined it in code.php

my_round_down(1234,$pre); // use this where you want, in or out the loop



#8 nawtwrong

nawtwrong
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 March 2006 - 09:42 AM

Thanks for your reply.

I've got the majority of this code working now, however for some reason, when I round certain numbers up or down I get the same number from the list, for example:


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;
}



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;
}

$number = 6987; //Or whatever number has been submitted from a form

$pre=array(5000,10000,15000,25000,50000,75000,100000,125000,200000,300000,400000,500000,
1500000);

$new_number_rounded_down = my_round_down($number,$pre);

$new_number_rounded_up = my_round($number,$pre);


echo $new_number_rounded_down; //Gives 5000

echo $new_number_rounded_up; //Gives 5000 (should give 10000)


If I swap $number for something like 92000 it gives the correct lower and higher rounded numbers.

I've had a look but not sure what i'm doing wrong. Any ideas?

Thanks








0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users