PDA

View Full Version : PHP Multipling Dollar Amounts



dyous87
September 3rd, 2009, 04:11 AM
I am having trouble with something which I assumed should be rather easy but I cannot seem to find the answer anywhere. I have two numbers stored in variables which are supposed to represent dollar value and quantity pulled from an html form. When I try multiplying the amounts I get a whole number instead of a decimal number. For example:



$price = 6.99;
$amount = 2;
$totalPrice = $price * $amount;
echo $totalPrice;


When I run this code the output is "12" instead of "13.98". What am I doing wrong. Any help would surely be greatly appreciated.

Best Regards,
David

Finalfantasykid
September 3rd, 2009, 04:19 AM
I might be wrong but it is possible that it is rounding because you used 2 instead of 2.0 or 2.00.

try this instead...


$price = 6.99;
$amount = 2.00;
$totalPrice = $price * $amount;
echo $totalPrice;

dyous87
September 3rd, 2009, 04:24 AM
Hmm I just tried that also but I get the same results...I don't understand what's going on.

rp3
September 3rd, 2009, 04:26 AM
Hmm I just tried that also but I get the same results...I don't understand what's going on.

maybe something like this will work

$formatted = number_format($number,2);

dyous87
September 3rd, 2009, 04:36 AM
Thanks rp3! I used the number_formated() function and it worked. This is what I had to do:



$price = 6.99;
$amount = 2;
$formatedPrice = number_format($price, 2, '.', '');
$formatedAmount = number_format($amount, 2, '.', '');
$totalPrice = $formatedPrice * $formatedAmount;
echo $totalPrice;


Thanks a lot for all your help!

David

rp3
September 3rd, 2009, 04:38 AM
Thanks rp3! I used the number_formated() function and it worked. This is what I had to do:



$price = 6.99;
$amount = 2;
$formatedPrice = number_format($price, 2, '.', '');
$formatedAmount = number_format($amount, 2, '.', '');
$totalPrice = $formatedPrice * $formatedAmount;
echo $totalPrice;


Thanks a lot for all your help!

David


Your welcome

Reiger
September 3rd, 2009, 04:48 AM
That is just plain weird. I don't recall ever having trouble like that with PHP, and just to check I ran PHP in interactive mode from the command line (php -a):


php > echo (6.99 *2);
13.98
php > $a = 6.99; $b = 2; echo $a *$b;
13.98

Lux Perpetua
September 3rd, 2009, 06:09 AM
I am having trouble with something which I assumed should be rather easy but I cannot seem to find the answer anywhere. I have two numbers stored in variables which are supposed to represent dollar value and quantity pulled from an html form. When I try multiplying the amounts I get a whole number instead of a decimal number. For example:



$price = 6.99;
$amount = 2;
$totalPrice = $price * $amount;
echo $totalPrice;


When I run this code the output is "12" instead of "13.98". What am I doing wrong. Any help would surely be greatly appreciated.

Best Regards,
David

FWIW, I just ran your code verbatim:
<?php
$price = 6.99;
$amount = 2;
$totalPrice = $price * $amount;
echo $totalPrice;
?>
Result:
13.98
Output of php -v:
PHP 5.2.10 with Suhosin-Patch 0.9.7 (cli) (built: Jul 14 2009 11:56:54)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies