[solved] Ronding up or down
Moderator: General Moderators
[solved] Ronding up or down
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
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.
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
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));
}Based on the numbers given (all dependent on the last digit for rounding)..
Works for all three values given.
Code: Select all
$value = 123;
echo badrounder($value);
function badrounder($value)
{
if (($value%10)=5){
return $value;
}
else {
return (round($value/10)*10);
}
}It's using short-hand "ternary" if..else statements, and nesting them.hairyjim wrote:Care to explain it?
Instead of writing this:
Code: Select all
if ($a==1) {
$b = 1;
} else {
$b = 2;
}Code: Select all
$b = ($a==1) ? 1 : 2 ;There's a good, short explanation of them here: http://www.developer.com/lang/php/article.php/947911
Cheers. PHP graphics stuff is my thing..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.