decimal formatting with sprintf?

decimal formatting with sprintf?

am 16.01.2008 16:55:41 von Law Poop

Hello all --

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
55.55: 55.55
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?

am 16.01.2008 18:03:20 von Toby A Inkster

lawpoop wrote:

> It does the trailing zeros correctly, but not the leading zeros. I would
> like 5.5 to become 05.50 .

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 2.6.17.14-mm-desktop-9mdvsmp, up 17 days, 4:16.]

Gnocchi all'Amatriciana al Forno
http://tobyinkster.co.uk/blog/2008/01/15/gnocchi-allamatrici ana/

Re: decimal formatting with sprintf?

am 16.01.2008 19:09:23 von Law Poop

On Jan 16, 11:03 am, Toby A Inkster
wrote:

>
> Try "%05.2f", because "05.50" is five characters in length.
>

Thanks, Tony! That's it exactly.