I am rewriting my company's web reporting framework at the moment. We have opted to use Google's charts API. So here is the issue: the chart API requires that you specify the range for each axis, based on the input data. So if you had 3 data points with values 1, 6 and 16, your "Y" axis range might be 0 and 16. However in order to draw axis labels with even, sensible values you might want to make the range 0-20 and have labels for 5, 10 and 15. So the problem is write a routine which calculates the nearest "sensible" number given any number. 16 gives 20, 465 gives 500, 67,584 gives 68,000 and so forth.
Here are my efforts, neither of which seem like the "right way":
function math_rndup( $num )
{
$magn = $num / 10;
$last = round( $magn );
$mult = 1;
while( $magn / 10 > 0.1 )
{
$last = round( $magn );
$magn /= 10;
$mult *= 10;
}
$max = $last * $mult;
return $max;
}
function string_rndup( $val )
{
return substr( $val, 0, 1 ) +1 . str_repeat( '0', strlen( substr( $val, 1 )));
}
Incidentally doing the string cast and manipulating it performs just as well as using math operators.