note 84780 modified in function.round by danbrown

From: Date: Wed, 30 Jul 2008 18:41:49 +0000
Subject: note 84780 modified in function.round by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142951@lists.php.net to get a copy of this message
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; } ?> --was-- I needed a round up function with precision which surprisingly did not exist (at least that I could see). This does the trick: 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; } http://php.net/manual/en/function.round.php

« previous php.notes (#142951) next »