Page 1 of 1

understanding dollar amount code

Posted: Mon Sep 28, 2009 8:21 pm
by solidsquirrell
Hi, im just starting off learning PHP, and stumbled up on this line of code. Any feedback most appreciated.

Dollar amount Code

Code: Select all

$price =25;
$f_price = sprintf("%01.2f",$price);
echo "$fprice";
I understand this code is correct, but Im just having trouble comprehending it.

I would like to know what ("%01 means. (as in what it stands for) I know that ("%01.2f" (highlighted in bold) places an extra " 0 " at the end of 25.000

sorry to ask such a noob question, im currently learning from PHP & MySQL for dummies book.

thx.

S.S.

Re: understanding dollar amount code

Posted: Mon Sep 28, 2009 8:55 pm
by requinix
solidsquirrell wrote:I would like to know what ("%01 means. (as in what it stands for) I know that ("%01.2f" (highlighted in bold) places an extra " 0 " at the end of 25.000
Actually, that's not right.

%..f is a placeholder. The 'f' says it's a floating-point number (as opposed to a string or an integer). The "01.2" is the formatting string
- If the first character is a 0 or space then it's to be used as padding
- The rest up to the . is the minimum number of characters (digits) to display, padded with spaces (unless told otherwise) if that minimum can't be met
- The .2 means two digits of precision = two digits to the right of the decimal

There's more explanation on the sprintf manual page.

Re: understanding dollar amount code

Posted: Mon Sep 28, 2009 9:11 pm
by solidsquirrell
oh ok, thanks for your input, and the helpful link.