note 79153 deleted from function.round by danbrown

From: Date: Sun, 17 May 2009 16:16:23 +0000
Subject: note 79153 deleted from function.round by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154781@lists.php.net to get a copy of this message
Note Submitter: xellisx at gmail dot com ---- [quote] This can be much more efficient: <?php function roundup ($value, $dp) { return ceil($value*pow(10, $dp))/pow(10, $dp); } function rounddown ($value, $dp) { return floor($value*pow(10, $dp))/pow(10, $dp); } ?> Much cleaner, isn't it ;)? [/quote] Unfortunately - it wasn't formatting the number right and you have to figure out which way you want to go. Using the code - I've come up with this: <php function fract_round($n, $dp) { if(round($n, $dp) > $n) { return number_format(ceil($n*pow(10, $dp))/pow(10,$dp),$dp); } else { return number_format(floor($n*pow(10,$dp))/pow(10,$dp),$dp); } } ?> Of course we could just do this: <?php function fract_round($n, $dp) { return number_format(round($n, $dp), $dp); } ?>

« previous php.notes (#154781) next »