note 79153 added to function.round

From: Date: Tue, 13 Nov 2007 23:58:31 +0000
Subject: note 79153 added to function.round
Groups: php.notes 
Request: Send a blank email to php-notes+get-132238@lists.php.net to get a copy of this message
[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); } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 64.81.121.210 ---- Manual Page -- http://www.php.net/manual/en/function.round.php Edit -- https://master.php.net/note/edit/79153 Del: integrated -- https://master.php.net/note/delete/79153/integrated Del: useless -- https://master.php.net/note/delete/79153/useless Del: bad code -- https://master.php.net/note/delete/79153/bad+code Del: spam -- https://master.php.net/note/delete/79153/spam Del: non-english -- https://master.php.net/note/delete/79153/non-english Del: in docs -- https://master.php.net/note/delete/79153/in+docs Del: other reasons-- https://master.php.net/note/delete/79153 Reject -- https://master.php.net/note/reject/79153 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132238) next »