Jump to content

[SOLVED] Always round down in 20's


jaymc

Recommended Posts

$num = floor($num/20) * 20;

 

Ah yes, thats how I used to do it

 

That works apart from when $num = any of these 20,40,60,80

 

I need it like this

 

Example

20 rounds to 0

40 rounds to 20

60 rounds to 40

80 rounds to 60

 

Any ideas to get around that issue?

$nums = array(0, 1, 7, 20, 40, 55);

foreach($nums as $key => $num)
{
    // check that numb is dividable by 20
    if(num >= 20 && ($num%20 == 0))
        $num -= 20;
    else
         $num = floor($num/20) * 20;

    $rounded[] = $num;
}

echo '<pre>' . print_r($rounded, true) . '</pre>';

 

 

$nums = array(0, 1, 7, 20, 40, 55);

foreach($nums as $key => $num)
{
    // check that numb is dividable by 20
    if(num >= 20 && ($num%20 == 0))
        $num -= 20;
    else
         $num = floor($num/20) * 20;

    $rounded[] = $num;
}

echo '<pre>' . print_r($rounded, true) . '</pre>';

 

That.. didnt work?

 

$nums = array(0, 1, 7, 20, 40, 55, 60, 80, 81); produced this..

 

Array

(

    [0] => 0

    [1] => 0

    [2] => 0

    [3] => 20

    [4] => 40

    [5] => 40

    [6] => 60

    [7] => 80

    [8] => 80

)

 

$num = floor($num/20) * 20;

 

Ah yes, thats how I used to do it

 

That works apart from when $num = any of these 20,40,60,80

 

I need it like this

 

Example

20 rounds to 0

40 rounds to 20

60 rounds to 40

80 rounds to 60

 

Any ideas to get around that issue?

 

Hey it's your project, so you must know what you're doing.  All I'm gonna say is...does 0 get rounded down to -20? So why would 20 get rounded down to 0, 40 to 20, etc...? Not necessarily arguing; just curious.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.