[solved] Ronding up or down

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

[solved] Ronding up or down

Post by hairyjim »

Hi,

I have a rounding question.

I understand the round function to the extent of what the manual tells me.

Is it possible to round the following:

123 = 120
125 = 125
126 = 130

See the pattern? Anything less than gets rounded down, if its a 5 keep the same if it is over 5 then round it up.

Could someone please advise.

Regards
Jim
Last edited by hairyjim on Thu Jul 14, 2005 3:33 am, edited 1 time in total.
jayloo
Forum Newbie
Posts: 11
Joined: Tue Jun 14, 2005 1:28 pm

Post by jayloo »

You're gonna have to write your own function to handle it.
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Cheers.

I was hoping for something more specific than that.

It is the logic of the code I do not know how to create.
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

I agree with jayloo. You really have to write you own function for that, or you could check http://www.hotscripts.com for an already written function... I wrote my own, but it's not the most pretty code since i'm not using OOP yet... LOL
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Code: Select all

function myround($var) {
    $remainder = $var%10; //Modulus .. handy operator.
    if ($remainder < 5) {
        return $var-$remainder;
    } elseif ($remainder > 5) {
        return $var+(10-$remainder);
    } elseif($remainder == 5) {
        return $var;
    } else {
        return -1;// Uh-oh
    }
}

Code: Select all

function myround_eviledition($v){
    return ($v%10>5)?$v+(10-($v%10)):(($v%10<5)?$v-($v%10):(($v%10==5)?$v:-1));
}
Hehehe...
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Based on the numbers given (all dependent on the last digit for rounding)..

Code: Select all

$value = 123;

echo badrounder($value);

function badrounder($value)
{
    if (($value%10)=5){
    return $value;
    }
    else {
    return (round($value/10)*10);
    }
}
Works for all three values given.
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Mr Inspiring & Mr Unique

You both have uniquely solved the problem and inspired me to read up on this % modulus operator thingy.

p.s I don't like eviledition Mr Inspiring onion :?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

hairyjim wrote:p.s I don't like eviledition Mr Inspiring onion :?
It's perfectly straightforward.

:wink: :D 8O
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Care to explain it?

p.s been on your ooerr website....loved the article on convolution.

Loved even more the 'spirograph'

do 1, 0.2, 5000 using lines. Then do the same except change the 5000 to 500.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

hairyjim wrote:Care to explain it?
It's using short-hand "ternary" if..else statements, and nesting them.

Instead of writing this:

Code: Select all

if ($a==1) {
  $b = 1;
} else {
  $b = 2;
}
you can write

Code: Select all

$b = ($a==1) ? 1 : 2 ;
So the ? is the equivalent of the 'if', and the : is the equivalant of the 'else'.

There's a good, short explanation of them here: http://www.developer.com/lang/php/article.php/947911
p.s been on your ooerr website....loved the article on convolution.

Loved even more the 'spirograph'

do 1, 0.2, 5000 using lines. Then do the same except change the 5000 to 500.
Cheers. PHP graphics stuff is my thing..
Post Reply