|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[1998-07-27 03:26 UTC] stef at moi dot org
$foo = 3;
$bar = sprintf("%.2d",$foo);
echo $bar;
should produce "03" not "3".
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 21:00:02 2025 UTC |
[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.