Reputation: 3729
I have been using echo "$".sprintf("%01\$.2f",$numvar);
for my USD formatting, but I only copied and pasted that. I looked over the documentation at php.net but it's still not clear to me.
Please help me understand what each part of "%01\$.2f"
does in that function.
Upvotes: 2
Views: 1551
Reputation: 51950
%
is the start of the conversion specification
01\$
signifies that the value will be placed in the first item of the result
More usually, the same would be written as 1$
.2
is the precision specifier, which dictates how many decimal digits should be displayed
f
means that the argument is treated as a float, and presented as a floating-point number (locale aware)
For full details of the above, and what is available, see the description of the format
parameter of sprintf()
.
Upvotes: 4
Reputation: 6891
The first argument of the sprint
function is the "format". The possible formats include:
Possible format values:
- %% - Returns a percent sign
- %b - Binary number
- %c - The character according to the ASCII value
- %d - Signed decimal number
- %e - Scientific notation (e.g. 1.2e+2)
- %u - Unsigned decimal number
- %f - Floating-point number (local settings aware)
- %F - Floating-point number (not local settings aware)
- %o - Octal number
- %s - String
- %x - Hexadecimal number (lowercase letters)
- %X - Hexadecimal number (uppercase letters)
Additional format values. These are placed between the % and the letter (example %.2f):
- "+" (Forces both + and - in front of numbers. By default, only negative numbers are marked)
- ' (Specifies what to use as padding. Default is space. Must be used together with the width specifier. Example: %'x20s (this uses "x" as padding)
- "-" (Left-justifies the variable value)
- [0-9] (Specifies the minimum width held of to the variable value)
- .[0-9] (Specifies the number of decimal digits or maximum string length)
Upvotes: 1