Re: Subtracting Money
| From: | Hugh Danaher | Date: | Fri, 15 Nov 2002 01:41:55 +0000 |
| Subject: | Re: Subtracting Money | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-124557@lists.php.net to get a copy of this message | ||
Try:
number_format()
Hugh
----- Original Message -----
From: "Sascha Cunz" <Sascha@GaNoAn.org>
To: "Stephen" <webmaster@melchior.us>; "PHP List"
<php-general@lists.php.net>
Sent: Thursday, November 14, 2002 5:37 PM
Subject: Re: [PHP] Subtracting Money
Hi Stephen,
> I'm having a problem. I need to subtract money, but if they type in an
> ammount such as $2.30 and I subtract $0.20, instead of returning $2.10
like
> it should, it returns $2.1. How can I fix this keeping in mind that other
> variable could be $2.40 or $100.30 and so on...?
This is because, 2.1 is equal to 2.10 :-) You need to format the numbers in
the way you want them.
have a look at:
http://www.php.net/manual/en/function.number-format.php
and/or
http://www.php.net/manual/en/function.sprintf.php
both of them should do that job.
sprintf's Example 6:
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
-Sascha
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php