Page 1 of 1

[solved] Ronding up or down

Posted: Wed Jul 13, 2005 9:55 am
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

Posted: Wed Jul 13, 2005 10:11 am
by jayloo
You're gonna have to write your own function to handle it.

Posted: Wed Jul 13, 2005 10:29 am
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.

Posted: Wed Jul 13, 2005 10:31 am
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

Posted: Wed Jul 13, 2005 11:23 am
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...

Posted: Wed Jul 13, 2005 12:41 pm
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.

Posted: Thu Jul 14, 2005 3:31 am
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 :?

Posted: Thu Jul 14, 2005 4:00 am
by onion2k
hairyjim wrote:p.s I don't like eviledition Mr Inspiring onion :?
It's perfectly straightforward.

:wink: :D 8O

Posted: Thu Jul 14, 2005 4:06 am
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.

Posted: Thu Jul 14, 2005 4:17 am
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..