#33644 [Opn->Bgs]: sprintf digit formating problem

From: Date: Mon, 11 Jul 2005 13:25:30 +0000
Subject: #33644 [Opn->Bgs]: sprintf digit formating problem
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-81675@lists.php.net to get a copy of this message
ID: 33644 Updated by: tony2001@php.net Reported By: root at sunstation dot com -Status: Open +Status: Bogus Bug Type: Unknown/Other Function Operating System: linux PHP Version: 4.3.11 New Comment: Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations. If you would like to know more about "floats" and what IEEE 754 is read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. Hint: <?php var_dump((int)((29/100)*100)); ?> Previous Comments: ------------------------------------------------------------------------ [2005-07-11 14:54:15] root at sunstation dot com Description: ------------ There is a problem with formatting float output for 0.28 values. Check it out. Reproduce code: --------------- <?php for($i=1;$i<=10000;$i++) { $a = round($i/100,2); if (sprintf("%d",$a*100) != $i) echo sprintf("$i -> %d<br>",$a*100); } ?> Expected result: ---------------- I would expect no output at all Actual result: -------------- 29 -> 28 57 -> 56 58 -> 57 113 -> 112 114 -> 113 115 -> 114 116 -> 115 201 -> 200 203 -> 202 205 -> 204 207 -> 206 226 -> 225 228 -> 227 230 -> 229 232 -> 231 251 -> 250 253 -> 252 255 -> 254 402 -> 401 406 -> 405 410 -> 409 414 -> 413 427 -> 426 431 -> 430 435 -> 434 439 -> 438 452 -> 451 456 -> 455 460 -> 459 464 -> 463 477 -> 476 481 -> 480 485 -> 484 489 -> 488 ... ... 8160 -> 8159 8171 -> 8170 8174 -> 8173 8182 -> 8181 8185 -> 8184 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=33644&edit=1

« previous php.bugs (#81675) next »