note 86424 added to function.floor
| From: | mikedotdeviseatgmaildotcom at osu1 dot php dot net | Date: | Sat, 18 Oct 2008 00:02:03 +0000 |
| Subject: | note 86424 added to function.floor | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145463@lists.php.net to get a copy of this message | ||
marc at moblaster's rounding technique for handling financial truncation fails for 0.0, and
probably has unintended consequences for values less than 0. Something like this should do the
trick:
function truncateMoneyFloat($amount)
{
if ($amount > 0.005) return round($amount - 0.005,2);
elseif ($amount < -0.005) return round($amount + 0.005,2);
else return 0.0;
}
However, +1 billion for jolyon at mways' comment regarding abandoning floats entirely when
processing financial data. Create a money class and track the value internally with an integer.
Provide methods to scale by float values where the rounding behavior can be controlled by an
explicit policy variable.
----
Server IP: 209.41.74.194
Probable Submitter: 24.22.117.179
----
Manual Page -- http://www.php.net/manual/en/function.floor.php
Edit -- https://master.php.net/note/edit/86424
Del: integrated -- https://master.php.net/note/delete/86424/integrated
Del: useless -- https://master.php.net/note/delete/86424/useless
Del: bad code -- https://master.php.net/note/delete/86424/bad+code
Del: spam -- https://master.php.net/note/delete/86424/spam
Del: non-english -- https://master.php.net/note/delete/86424/non-english
Del: in docs -- https://master.php.net/note/delete/86424/in+docs
Del: other reasons-- https://master.php.net/note/delete/86424
Reject -- https://master.php.net/note/reject/86424
Search -- https://master.php.net/manage/user-notes.php