php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #583 sprintf not allowing zero padding on decimal numbers
Submitted: 1998-07-27 03:26 UTC Modified: 1998-09-10 17:50 UTC
From: stef at moi dot org Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0 Final Release OS: FreeBSD 3.0
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: stef at moi dot org
New email:
PHP Version: OS:

 

 [1998-07-27 03:26 UTC] stef at moi dot org
$foo = 3;
$bar = sprintf("%.2d",$foo);
echo $bar;

should produce "03" not "3".

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1998-09-10 17:50 UTC] zeev
[jim]
Unfortunately, supporting %.# for decimal numbers isn't
quite trivial -- for example, sprintf("%3.2d", 8) produces
" 08". (Not that this can't or shouldn't be supported,
just making note of the fact that it isn't trivial and
requires handling cases like that.)

[ssb]
PHP's formatter is a bit different from that of libc here.
Everything after the "." in the formatting specifier
applies to the decimal part of a floating-point number,
(or the truncation length for strings).  Won't
sprintf("%02d", $foo) do what you want, or have I
misunderstood your problem?

[zeev]
this has been lingering for too long, Stig's solution works perfectly.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jun 17 07:01:32 2025 UTC