[thelist] php - rounding decimals help

Debes, Brandon bdebes at bellarmine.edu
Thu Oct 28 09:35:43 CDT 2004


> round up to one decimal place eg 3.34 is rounded to 3.4.

Why not just write a quick function like this:

<?php
function roundup ($num, $digits = 2) {
    $shift = pow(10 , $digits);
    return ((ceil($num * $shift)) / $shift);
}

print roundup(3.342, 1);	# 3.4
print roundup(3.342, 2);	# 3.35

?>

Brandon


More information about the thelist mailing list