note 50448 added to function.ceil

From: Date: Mon, 28 Feb 2005 20:40:48 +0000
Subject: note 50448 added to function.ceil
Groups: php.notes 
Request: Send a blank email to php-notes+get-85690@lists.php.net to get a copy of this message
I couldn't find any functions to do what ceiling does while still leaving I specified number of decimal places, so I wrote a couple functions myself. round_up is like ceil but allows you to specify a number of decimal places. round_out does the same, but rounds away from zero. <?php // round_up: // rounds up a float to a specified number of decimal places // (basically acts like ceil() but allows for decimal places) function round_up ($value, $places=0) { if ($places < 0) { $places = 0; } $mult = pow(10, $places); return ceil($value * $mult) / $mult; } // round_out: // rounds a float away from zero to a specified number of decimal places function round_out ($value, $places=0) { if ($places < 0) { $places = 0; } $mult = pow(10, $places); return ($value >= 0 ? ceil($value * $mult):floor($value * $mult)) / $mult; } echo round_up (56.77001, 2); // displays 56.78 echo round_up (-0.453001, 4); // displays -0.453 echo round_out (56.77001, 2); // displays 56.78 echo round_out (-0.453001, 4); // displays -0.4531 ?> ---- Manual Page -- http://www.php.net/manual/en/function.ceil.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50448 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50448&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50448&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50448&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50448&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50448&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50448&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#85690) next »