note 117341 added to function.ceil
| From: | BasVijfwinkel | Date: | Mon, 25 May 2015 03:38:22 +0000 |
| Subject: | note 117341 added to function.ceil | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202655@lists.php.net to get a copy of this message | ||
Note that 'ceil' can show unexpected behaviour when using float values due to inaccuracies
in the float system and PHP tendency to freely limiting the number of digits it displays to the
user.
It might like it is rounding up values that end in 0 (zero) but actually PHP is not showing that the
value is not exactly the value it shows.
<?PHP
echo('Displaying 13915.0000000000018190 : '. 13915.0000000000018190);
echo('<br>');
echo('Result ceil(13915.0000000000018190) :'.ceil(13915.0000000000018190));
echo('<br>');
?>
For example 13915 cannot be represented as exactly 13915 but becomes
13916.0000000000018190 and 'ceil' will therefore round it up to 13916.
<?PHP
$value = 12650 * 1.1;
echo('Expected behaviour : ceil(12650 * 1.1) = ceil(13915) = 13915');
echo('<br>');
echo('Using only ceil :'.ceil($value));
echo('<br>');
echo('Showing decimals : '.number_format($value - floor($value), 16));
echo('<br>');
echo('Using ceil + round : '.ceil(round($value,4)));
?>
So if 'ceil' looks like it is not working correctly and rounding up figures that end in 0
(zero) then this might be the cause of this behaviour.
Adding a simple 'round' before the applying the 'ceil' solves this problem.
----
Server IP: 72.52.91.14
Probable Submitter: 124.39.122.119
----
Manual Page -- http://php.net/manual/en/function.ceil.php
Edit -- https://master.php.net/note/edit/117341
Del: integrated -- https://master.php.net/note/delete/117341/integrated
Del: useless -- https://master.php.net/note/delete/117341/useless
Del: bad code -- https://master.php.net/note/delete/117341/bad+code
Del: spam -- https://master.php.net/note/delete/117341/spam
Del: non-english -- https://master.php.net/note/delete/117341/non-english
Del: in docs -- https://master.php.net/note/delete/117341/in+docs
Del: other reasons-- https://master.php.net/note/delete/117341
Reject -- https://master.php.net/note/reject/117341
Search -- https://master.php.net/manage/user-notes.php