Subscribe to RSS Subscribe to Comments

Alan Blaine Whitney

PHP Oddity

This was tested on PHP 5.2.6 and 5.2.10, you mileage may actually vary.

 <?php
$number = 20864.01;
$num = $number * 100;
echo intval($num);
?>

If such a piece of code was ran, but would one expect, 2086401 maybe. I would, but you actually get 2086400. Strange.

<?php
$number = 20864.01;
$num = $number * 100;
echo intval(round($num));
?>

That will give you 2086401.

UPDATE: I guess this problem is not a PHP per se but a glibc issue.

Comments

  1. March 19th, 2010 | 9:13 am

    Interesting! So what is QuickBooks using for libraries? LOL

  2. Andreas
    April 26th, 2010 | 6:38 pm

    In fact this is just floating point arithmetic. 20864.01 * 100 is something about 208640.099999999976716936 in floating point, so the behavior of intval() is just fine. Your example will show the same result in any progamming language that uses IEEE 754 floating point format.

Leave a reply

Based on FluidityTheme Redesigned by Kaushal Sheth Sponsored by Send Flowers