Jump to content

[SOLVED] Always round down in 20's


jaymc

Recommended Posts

Do you know how I can always round down to the nearest 20, starting from 0,20,40,60 and so on

 

Example

0 rounds to 0

7 rounds to 0

19 rounds to 0

20 rounds to 0

21 rounds to 20

36 rounds to 20

44 rounds to 40

88 rounds to 80

 

And so on..

 

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

$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>';

 

 

Link to comment
Share on other sites

$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

)

 

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.