php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31180 4.3.10 printf(%.2f) precision bug.
Submitted: 2004-12-18 12:18 UTC Modified: 2004-12-19 21:06 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: patrick at ilmander dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 4.3.10 OS: Red Hat 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: patrick at ilmander dot com
New email:
PHP Version: OS:

 

 [2004-12-18 12:18 UTC] patrick at ilmander dot com
Description:
------------
Printing a variable with %.2f should output the number with 2 decimals. After upgrading to 4.3.10 it now outputs 3 decimals. %.1f outputs with 2 decimals and I haven't figured out how to print out just 1 decimal (%.1f doesn't work), so I'm treating this as a bug.

Reproduce code:
---------------
printf("%.2f", $member[score] / $member[votes]);

Expected result:
----------------
1.23

Actual result:
--------------
1.234

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-18 12:20 UTC] patrick at ilmander dot com
Printing a variable with %.2f should output the number with 2 decimals. After upgrading to 4.3.10 it now outputs 3 decimals. %.1f outputs with 2 decimals and I haven't figured out how to print out just 1 decimal (%.0f doesn't work), so I'm treating this as a bug.
 [2004-12-18 23:43 UTC] derick@php.net
This works fine here:

$ php-4.3dev -r 'echo sprintf("%.2f", 42.1231213123);'

42.12
 [2004-12-19 16:54 UTC] postings-php-bug at hans-spath dot de
I've tried it under PHP 4.3.10/Linux 2.6 and PHP 4.3.8/WinXPsp2, works as it should.

Could you test with "_%.2f_", just to get sure the last digit isn't coming from somewhere else in your code?
 [2004-12-19 19:18 UTC] iliaa@php.net
works fine.
 [2004-12-19 21:06 UTC] patrick at ilmander dot com
I'm sorry, I should have given you more details. The problem doesn't seem to be printf() itself, but where I used it. I was using the following code:
"Rating: <b><?=printf("%.2f", $rating)?></b>"
which worked fine in 4.3.4, but when we upgraded to 4.3.10 it produced the stated error. I've updated all my code to
"Rating: <b><?php printf("%.2f", $rating)?></b>"
which works fine. I probably shouldn't have used printf() inside a <?= ?> in the first place, but it worked fine in 4.3.4 and not in 4.3.10, that's why I reported it as a bug.

Sorry for wasting your time.

// Patrick
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 20:01:35 2025 UTC