NYCPHP Meetup

NYPHP.org

[nycphp-talk] Rounding floats

Rob Marscher rmarscher at beaffinitive.com
Tue Sep 14 16:19:09 EDT 2010


On Sep 14, 2010, at 3:57 PM, Chris Snyder wrote:
...snip...
> I'm seeing things like:
...snip...
> $total = round( round( $total, 2 ) + round( $rec['amt'], 2 ), 2 );
...snip...
> Is it safe to get rid of this round( round() + round() ) pattern?

If you change it, you'll get different results in certain cases.

Here's a simple example.
$total = .004;
$rec['amt'] = .003;
echo round( round( $total, 2 ) + round( $rec['amt'], 2 ), 2 );
// prints 0
echo round( $total + $rec['amt'], 2 );
// prints 0.01

-Rob






More information about the talk mailing list