sorry for jumping right in with a question but this is really annoying me... ive been everywhere looking for the answer to this..
im building a shopping cart but the only money value the ecommerce company will accept is in the form of 0000.00
for example
$100 would be 0100.00
$100.35 would 0100.35
my problem is how do i make sure that the value i send to the ecommerce company is always in the correct format
what if i get a value of $1.20 or something?
how do i make 1.20 into 0001.20
how can i check the string for the right format and add the zeros where and when necesary?
please help
ive looked at http://uk2.php.net/manual/en/function.number-format.php
and
http://nl2.php.net/manual/nl/function.money-format.php
but all both seem to do is move the decimal point.
am i missing something?
any value to 0000.00
Moderator: General Moderators
look at http://www.php.net/sprintf
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
number_format()
Edit | Have yourt looked at money_format() Specifically these two examples...
Edit | Have yourt looked at money_format() Specifically these two examples...
Code: Select all
<?php
// US national format, using () for negative numbers
// and 10 digits for left precision
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#10n', $number) . "\n";
// ($ 1,234.57)
// Similar format as above, adding the use of 2 digits of right
// precision and '*' as a fill character
echo money_format('%=*(#10.2n', $number) . "\n";
// ($********1,234.57)
?>thanks all for your help
thanks all for your help
Everah.. you hit the nail on the head (almost)
thank you
what i finally came up with (after your guidance) is
which does exactly what i need
thanks all i appreciate it
Everah.. you hit the nail on the head (almost)
thank you
what i finally came up with (after your guidance) is
Code: Select all
money_format('%=0(#4.2n', $number) . "\n";thanks all i appreciate it
- stereofrog
- Forum Contributor
- Posts: 386
- Joined: Mon Dec 04, 2006 6:10 am
Code: Select all
$formatted = sprintf("%07.2f", $money);upd: be aware that money_format is locale-dependent and not portable.