any value to 0000.00

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

Post Reply
ozonew4m
Forum Newbie
Posts: 2
Joined: Fri Mar 16, 2007 10:02 am

any value to 0000.00

Post 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?
mentor
Forum Contributor
Posts: 100
Joined: Sun Mar 11, 2007 11:10 am
Location: Pakistan

Post by mentor »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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)
?>
ozonew4m
Forum Newbie
Posts: 2
Joined: Fri Mar 16, 2007 10:02 am

thanks all for your help

Post 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:
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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.
Post Reply