NYCPHP Meetup

NYPHP.org

[nycphp-talk] Unfriendly Float Handling

csnyder chsnyder at gmail.com
Fri Mar 31 19:03:04 EST 2006


On 3/31/06, Adam Maccabee Trachtenberg <adam at trachtenberg.com> wrote:
> On Fri, 31 Mar 2006, tedd wrote:
>
> > if ($a < $b + .01 && $a > $b - .01)
>
> I've found for me a more clear way to do this is:
>
> define('epsilon', 0.01);
>
> if (abs($a - $b) < epsilon) {
>
> But I don't feel strongly about it. :)
>
> -adam


You definitely need to have a bag of tricks when you start dealing
with monetary values (or other floats) in php. I felt exactly the same
frustration the first time I crossed over from content management into
commerce: four years of writing php code and I had never used
number_format().

Are there any good articles or tipsheets out there on working with
money in php? If not, maybe there should be a Financial Phundamentals
to help get other developers up to speed on the best practices for
being arbitrarily precise.

--
Chris Snyder
http://chxo.com/


More information about the talk mailing list