tags:

views:

200

answers:

4

I cannot seem to figure out how to always round up in PHP. ceil() would be the obvious choice but I need the same functionality that round() provides with its second parameter "precision". Here is an example:

// Desired result: 6250
echo round(6244.64, -2); // returns 6200
echo round(6244.64, -1); // returns 6240
echo ceil(6244.64) // returns 6245

I need the number to always round up so that I can calculate an equal divisor based on parameters for a chart.

+2  A: 

From a comment on http://php.net/manual/en/function.ceil.php:

Quick and dirty `ceil` type function with precision capability.

function ceiling($value, $precision = 0) {
    return ceil($value * pow(10, $precision)) / pow(10, $precision);
}
Mark Byers
I think the operation order is mixed up, you'd need to divide before ceil, then multiply, not the other way around, no?
njk
@njk: ceiling(6244.64, -1) gives the desired result 6250. Notice that the precision is negative in this case.
Mark Byers
A: 

You could divide by 10, ceil(); and then multiply by ten

meosoft
A: 

Already answered here

Daniel
A: 
echo 10*ceil($number/10);
njk