Marc Carlucci
1

Marc Carlucci

IT Consultant @ Freelance (Self-employed)
Male, 30 years old,

Wallingford

,

United Kingdom

 

Float operation returns weird result

At work I had an issue with a float operation this <?php echo(859-832.35-26.65) ?> would return -2.1316282072803E-14 not the zero I'm used to :)

After reporting it as a bug, I got a reply with an explanation.

The bottom line is always format your float operations (and others processed data) to what you want to show.
In that case <?php echo sprintf('%d',859-832.35-26.65) ?> returns 0 but <?php echo sprintf('%.2f',859-832.35-26.65) ?> returns -0.00 which is a bit weird but linked to the very nature of floats (see the reference document on the bug report).

Anyway it's good to have an explanation, thanks to The Pimp !