php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33644 sprintf digit formating problem
Submitted: 2005-07-11 14:54 UTC Modified: 2005-07-11 15:25 UTC
From: root at sunstation dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 4.3.11 OS: linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: root at sunstation dot com
New email:
PHP Version: OS:

 

 [2005-07-11 14:54 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-11 15:25 UTC] tony2001@php.net
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));
?>
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 12:00:02 2026 UTC