I'm looking for an elegant way to rounding up decimal numbers always up. Seems like round(0.0045001, 5, PHP_ROUND_HALF_UP);
is not returning what I expected so I came up with following function;
function roundUp($number, $precision) {
$rounded = round($number, $precision);
if ($rounded < $number) {
$add = '0';
for ($i = 1; $i <= $precision; $i++) {
if ($i == 1) $add = '.';
$add .= ($i == $precision) ? '1' : '0';
}
$add = (float) $add;
$rounded = $rounded + $add;
}
return $rounded;
}
I was wondering if there is any other, more elegant way to achieve this?
Expected Result :
var_dump(roundUp(0.0045001, 5)) // 0.00451;