Re: Subtracting Money

From: Date: Fri, 15 Nov 2002 01:37:55 +0000
Subject: Re: Subtracting Money
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-124555@lists.php.net to get a copy of this message
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

« previous php.general (#124555) next »