note 84780 deleted from function.round by googleguy
| From: | googleguy@php.net | Date: | Sun, 09 Dec 2012 08:23:09 +0000 |
| Subject: | note 84780 deleted from function.round by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192416@lists.php.net to get a copy of this message | ||
Note Submitter: Darryl Kuhn
----
I needed a round up function with precision which surprisingly did not exist (at least that I could
see). This does the trick:
<?php
function roundUp( $value, $precision=0 )
{
// If the precision is 0 then default the factor to 1, otherwise
// use 10^$precision. This effectively shifts the decimal point to the
// right.
if ( $precision == 0 ) {
$precisionFactor = 1;
}
else {
$precisionFactor = pow( 10, $precision );
}
// ceil doesn't have any notion of precision, so by multiplying by
// the right factor and then dividing by the same factor we
// emulate a precision
return ceil( $value * $precisionFactor )/$precisionFactor;
}
?>