Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- decimal formatting with sprintf?
January 16, 2008, 3:55 pm
rate this thread
I'm trying to format decimal numbers for a report. I would have to
have the number with leading zeros to the tens' place, and trailing
zeros to the thousandths' place.
I'm using sprintf("%02.2f", $num) to get the following results:
0.5 : 0.50
0.55 : 0.55
5.5 : 5.50
55.5 : 55.50
100 : 100.00
It does the trailing zeros correctly, but not the leading zeros. I
would like 5.5 to become 05.50 .
I don't really understand the syntax of the format specifier of
sprintf. Can someone help me out or point me to a good tutorial?
Re: decimal formatting with sprintf?
Try "%05.2f", because "05.50" is five characters in length.
Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux 18.104.22.168-mm-desktop-9mdvsmp, up 17 days, 4:16.]
Gnocchi all'Amatriciana al Forno