PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
neophyte
DevNet Resident
Posts: 1537 Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota
Post
by neophyte » Tue Jan 16, 2007 5:11 pm
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().
neophyte
DevNet Resident
Posts: 1537 Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota
Post
by neophyte » Tue Jan 16, 2007 5:19 pm
If I'm barking up the wrong tree go ahead and say Yo! You can't do it with sprintf.
Kieran Huggins
DevNet Master
Posts: 3635 Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:
Post
by Kieran Huggins » Tue Jan 16, 2007 5:20 pm
I'm confused... is this just a puzzle? or are you actually looking for a solution?
neophyte
DevNet Resident
Posts: 1537 Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota
Post
by neophyte » Tue Jan 16, 2007 5:23 pm
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...
Kieran Huggins
DevNet Master
Posts: 3635 Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:
Post
by Kieran Huggins » Tue Jan 16, 2007 5:26 pm
if
money_format() isn't general enough for you, have you considered preg_replace() ?
also check out number_format()
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jan 16, 2007 5:34 pm
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
neophyte
DevNet Resident
Posts: 1537 Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota
Post
by neophyte » Tue Jan 16, 2007 5:37 pm
Hmm thanks for the advice feyd.