note 86424 deleted from function.floor by danbrown
| From: | danbrown@php.net | Date: | Thu, 10 Mar 2011 15:20:25 +0000 |
| Subject: | note 86424 deleted from function.floor by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177755@lists.php.net to get a copy of this message | ||
Note Submitter: mike dot devise at gmail dot com
----
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.