Sep 22, 2001, 7:02 AM
Post #2 of 3
If you only deal with integers, that's easy. Just use
This format string will tell sprintf to reserve 7 digits for $num and fill it up with leading zeroes. So that's more or less what I guess you need from your examples.
If you have to deal with floating point values, you've got to know the number of digits following the decimal point. If you want, say, 7 digits in total, including the decimal point, and 2 digits after the decimal point, and leading zeroes, you'd have to write
So the format is basically:
1. a percent sign '%'
2. an optional plus sign '+' to indicate you want +/- printed for each number
3. an optional minus sign to indicate left alignment
4. an optional zero to indicate you want leading zeroes instead of leading spaces
5. the (optional) number of total digits (characters), including sign / decimal point
6. for [efg] a dot followed by the number of digits after the decimal point
7. a type specifier for the argument (e.g. 'd' for integers, 'f' for floats, 'x' for hex...)
...I just realized that I could have copypasted part of the above directly from the manpage...
So, if you want to read the above rules in a bit more detail, just look at perldoc -f sprintf, or get a good book about C programming. (Kernighan/Ritchie also explain printf() format strings in their book.)
Hope this helps.