Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.