Page 1 of 1
Any Ideas?
Posted: Tue Jan 11, 2005 7:52 pm
by patch2112
Hello All,
I'm getting a problem on a simple adding equation and I just can't figure it out.
The problem only occurs when the result ($total_item_price) exceeds 1000. All are decimal numbers.
Code: Select all
$price = $_POSTї'quote'];
$text_price = $_POSTї'real_text_cost'];
$digi_price = $_POSTї'digi_price'];
echo(" Price: $price<br>
Text Price: $text_price<br>
Digi Price: $digi_price");
#add text_price and rest of $price
$total_item_price = (($price * 1) + ($text_price * 1) + ($digi_price * 1));
echo(" Total Item Price: $total_item_price");
The first echo is verifing that the $_POST's were received okay, and it is. The '* 1's are just to ensure it's a number.
Thanks a bunch,
Philip
Posted: Tue Jan 11, 2005 8:10 pm
by feyd
what specifically is the problem? I don't quite understand..

Posted: Wed Jan 12, 2005 12:57 am
by AGISB
How does the *1 makes sure it is a number? I would take that out as it does nothing in my opinion.
Anyway you should give us a little more info. Please post an example with 3 values and what the adding gives you.
Thanks
Posted: Wed Jan 12, 2005 5:13 am
by patch2112
Thanks for your responses, I'm in England, so I'm just getting up and seeing them.
Examples:
$price = 1,368.57
$text_price = 267.75
$digi_price = 100.00
Outputs $total_item_price = 368.75
$price = 28.60
$text_price = 21.00
$digi_price = 100.00
Outputs $total_item_price = 149.60
The * 1's are to ensure that it treats the variable as a number and not a string.
Thanks again,
Philip
Posted: Wed Jan 12, 2005 6:10 am
by AGISB
Ok the problem is obvious:
(1,368.57 * 1) + (267.75 * 1) + (100.00 * 1) = 368.75
1,368.57 * 1 = 1 as , is no value and the rest is cut of
just get the , out of your input and you are set
Posted: Wed Jan 12, 2005 6:42 am
by patch2112
Thanks AGISB,
I'm very very new to all of this, so nothing is obvious to me yet...lol.
I understand what you were saying though and found this javascript in a function that someone else wrote...
Code: Select all
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}
When I got rid of it, the equation worked fine.
I am a little confused on the * 1 stuff still if you have a minute. I picked up the idea of performing a mathematic function on a variable to ensure it was being treated as a number as PHP doesn't differentiate between numeric and string variables. So....
$var1 = 1
$var2 = 2
$var1 + $var2 = 3, instead of 12, which it has done before. I understand this isn't the case in higher level programming languages where you specify the variable type.
Any thoughts? Great. If not, thanks for leading me in the right direction!
Philip
Posted: Wed Jan 12, 2005 6:54 am
by AGISB
You probaly come from Basic do you?
+ is used in basic to combine 2 strings as well as mathematical add.
in php + is used as add and not as string combine. This would be
$result would be: 12
$result would be 3
Posted: Wed Jan 12, 2005 6:58 am
by patch2112
Ah-HAH!
No, I don't know basic either. I'm learning Javascript and PHP at the same time. Javascript + can be used for either, and I must have confused them.
Thanks a bunch, and I formally apologize for being an idiot.
Philip