Page 1 of 1
take the sprintf challenge
Posted: Tue Jan 16, 2007 5:11 pm
by neophyte
Question one -- no clue how to make this work.
Integer or floating point with comma seperated thousands
1,200,300.00
Question 2 -- Add a dollar sign to the front of a number with two decimals. Here's what I'm working with:
The end product should be $12.00
rules -- no string concatenation, or other functions, just sprintf().
Posted: Tue Jan 16, 2007 5:19 pm
by neophyte
If I'm barking up the wrong tree go ahead and say Yo! You can't do it with sprintf.
Posted: Tue Jan 16, 2007 5:20 pm
by Kieran Huggins
I'm confused... is this just a puzzle? or are you actually looking for a solution?
Posted: Tue Jan 16, 2007 5:23 pm
by neophyte
I'm actually looking for a solution. The problem is that I need to be able to format numeric strings in specific ways that the client code specifies. I'm looking at sprintf but maybe I should be creating my own syntax...
Posted: Tue Jan 16, 2007 5:26 pm
by Kieran Huggins
if
money_format() isn't general enough for you, have you considered preg_replace() ?
also check out number_format()
Posted: Tue Jan 16, 2007 5:34 pm
by feyd
sprintf() won't handle thousands separation, but can do the basic money:
Code: Select all
[feyd@home]>php -r "printf('$%1.2f', 12.34);"
$12.34
[feyd@home]>php -r "printf('$%1.2f', 12.00);"
$12.00
[feyd@home]>php -r "printf('$%1.2f', 12);"
$12.00
[feyd@home]>php -r "printf('$%1.2f', 12.1);"
$12.10
[feyd@home]>php -r "printf('$%1.2f', .12);"
$0.12
Posted: Tue Jan 16, 2007 5:37 pm
by neophyte
Hmm thanks for the advice feyd.