Page 1 of 1

any value to 0000.00

Posted: Fri Mar 16, 2007 10:09 am
by ozonew4m
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?

Posted: Fri Mar 16, 2007 10:22 am
by mentor

Posted: Fri Mar 16, 2007 10:31 am
by RobertGonzalez
number_format()

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

Posted: Fri Mar 16, 2007 10:51 am
by ozonew4m
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

Code: Select all

money_format('%=0(#4.2n', $number) . "\n";
which does exactly what i need

thanks all i appreciate it :wink:

Posted: Fri Mar 16, 2007 10:55 am
by stereofrog

Code: Select all

$formatted = sprintf("%07.2f", $money);
BTW, you shouldn't use floats for money values.

upd: be aware that money_format is locale-dependent and not portable.