note 93339 added to function.round
| From: | BigueNique at yahoo dot ca | Date: | Fri, 04 Sep 2009 00:12:17 +0000 |
| Subject: | note 93339 added to function.round | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160208@lists.php.net to get a copy of this message | ||
For you folks who are still using PHP4, or just want their code to stay backward compatible,
here's a dirty trick to simulate the PHP_ROUND_HALF_UP mode.
By default, round() will round down (PHP_ROUND_HALF_DOWN):
<?php
echo round(0.5); // gives 0
?>
To simulate round up, simply add the biggest rounded power of 10, like this:
<?php
echo round(0.5 + 0.1); // gives 1 instead of 0
echo round(1.45/10 + 0.001, 2); // gives 0.15 instead of 0.14
?>
This could be generalized:
<?php
function round_up($val, $precision) {
return round($val + 10^(-($precision+1)), $precision);
}
?>
----
Server IP: 216.235.13.26
Probable Submitter: 216.239.78.133
----
Manual Page -- http://www.php.net/manual/en/function.round.php
Edit -- https://master.php.net/note/edit/93339
Del: integrated -- https://master.php.net/note/delete/93339/integrated
Del: useless -- https://master.php.net/note/delete/93339/useless
Del: bad code -- https://master.php.net/note/delete/93339/bad+code
Del: spam -- https://master.php.net/note/delete/93339/spam
Del: non-english -- https://master.php.net/note/delete/93339/non-english
Del: in docs -- https://master.php.net/note/delete/93339/in+docs
Del: other reasons-- https://master.php.net/note/delete/93339
Reject -- https://master.php.net/note/reject/93339
Search -- https://master.php.net/manage/user-notes.php