Any Ideas?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

Any Ideas?

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what specifically is the problem? I don't quite understand.. :?
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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.
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

Thanks

Post 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
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

Post 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) &#123;
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);
&#125;
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
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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

Code: Select all

$reslult = $var1.$var2
$result would be: 12

Code: Select all

$reslult = $var1 + $var2
$result would be 3
patch2112
Forum Commoner
Posts: 86
Joined: Sun Oct 31, 2004 9:44 am
Location: London

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