Round number to nearest twenty...


#1

Hey,

What I want to do is be able to round the value of a variable (always will be a number) to the nearest 20…

Like, if the variable is 56, it would round to 60. Or say the variable is 29, it would round to 20, but if it’s 30 it would round to 40. Or if it was 9 it would round to 0. Etc…

Any ideas?

Thanks,

Nick


#2

If you check the function “ROUND” on php.net it may be able ot help, however I suspect that you’ll need to manually create a function to handle that request…


#3

Hmmm - maybe divide by 20 to get the whole number - then mod it by 20 to see if you have to bump it up to the next level.

Ex:
[php]
$startValue = (int) $input/20;
$finalValue = ($input%20 >9)? ($startValue*20 + 20) : ($startValue * 20);
[/php]

Reference:
http://www.php.net/manual/en/language.o … hmetic.php
http://www.php.net/manual/en/language.t … ypecasting
Ternary - http://www.php.net/operators.comparison


#4

Go LIG Go… (I love the tertiary functions) 8)