|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-03-22 14:23 UTC] tony2001@php.net
[2007-03-22 14:42 UTC] fizz at beyond dot hjsoft dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 11:00:01 2025 UTC |
Description: ------------ When printing out a floating point number using printf (or storing it with sprintf, or casting it as an int) it gives an incorrect number. This appears to be due to floating point math being inaccurate. Reproduce code: --------------- <?php $val = 309; for ($x = 0; $x < 950; $x++) $val += 0.001; var_dump($val); echo $val . "\n"; $val = $val * 100; var_dump($val); echo $val . "\n"; printf ("%d\n", $val); echo (int)$val . "\n"; ?> Expected result: ---------------- float(309.95) 309.95 float(30995) 30995 30995 30995 Actual result: -------------- float(309.95) 309.95 float(30995) 30995 30994 30994