Re: newbie question
| From: | Eugene Lee | Date: | Sun, 12 Oct 2003 22:36:28 +0000 |
| Subject: | Re: newbie question | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-165925@lists.php.net to get a copy of this message | ||
On Mon, Oct 13, 2003 at 03:23:53AM +1000, Wang Feng wrote:
:
: "1. An optional padding specifier that says what character will be used for
: padding the results to the right string size. This may be a space character
: or a 0 (zero character). The default is to pad with spaces. An alternate
: padding character can be specified by prefixing it with a single quote (').
: See the examples below."
: "3. An optional number, a width specifier that says how many characters
: (minimum) this conversion should result in."
: ---- http://au.php.net/manual/en/function.sprintf.php
:
: Assume that $price=.65; then the "%0.2f" yields 0.65.
:
: If we follow what the manual says, then can you tell me what the 0 is used
: for? Is it a (optional) paddinng spcifier OR is it a (optional) width
: specifier OR both? And why does it yiled 0.65 rather than .65?
:
: (The manual doesn't explain things clear, man.)
The PHP manual is vague in several sections. I wonder how bug reports
get submitted for it?
The optional specifiers to the left of the decimal place have a psuedo
last-to-first precedence. For example:
$price = .65;
printf("'%8.2f'\n", $price);
-> ' 0.65'
This shows that there are 8 characters reserved for the number to the
left of the decimal. Therefore, '8' is the width specifier.
printf("'%-8.2f'\n", $price);
-> '0.65 '
printf("'%08.2f'\n", $price);
-> '00000000.65'
printf("'%0-8.2f'\n", $price);
-> '0.650000000'