This is a discussion on decimal formatting with sprintf? within the PHP Language forums, part of the PHP Programming Forums category; Hello all -- I'm trying to format decimal numbers for a report. I would have to have the number with ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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? |
|
|||
|
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/0...llamatriciana/ |