note 109352 added to function.round
| From: | agroveland at gmail dot com | Date: | Mon, 09 Jul 2012 20:17:14 +0000 |
| Subject: | note 109352 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189080@lists.php.net to get a copy of this message | ||
Re: chop a number to x decimal places without rounding up. You can subtract a cheat value (.5 * the
smallest decimal place you want) then round as normal. So if you want 3 decimal places, you
subtract 0.0005. Rounds to negative decimal place also.
<?php
function chopRound($n, $p){
return round($n-(5/(pow(10,$p+1))), $p);
}
echo chopRound(0.164999, 3); // 0.164
echo chopRound(3599.123, -2); // 3500
?>
In the first example, if you add enough 9's onto the number, it will eventually return 0.165.
Still, it's pretty quick.
----
Server IP: 69.147.83.197
Probable Submitter: 76.119.132.7
----
Manual Page -- http://www.php.net/manual/en/function.round.php
Edit -- https://master.php.net/note/edit/109352
Del: integrated -- https://master.php.net/note/delete/109352/integrated
Del: useless -- https://master.php.net/note/delete/109352/useless
Del: bad code -- https://master.php.net/note/delete/109352/bad+code
Del: spam -- https://master.php.net/note/delete/109352/spam
Del: non-english -- https://master.php.net/note/delete/109352/non-english
Del: in docs -- https://master.php.net/note/delete/109352/in+docs
Del: other reasons-- https://master.php.net/note/delete/109352
Reject -- https://master.php.net/note/reject/109352
Search -- https://master.php.net/manage/user-notes.php