Gossamer Forum
Home : Products : Links 2.0 : Customization :

help with calculations

Quote Reply
help with calculations
Ok, I'm doing some calculations with fields, but I'm always ending up with a negative number. I can't figure out why it's always showing a - sign in front of the Total value. Lets say 'Field1' is 10 and 'Field2' is 5. Now you would think, the 'Total' value would be 5, but it keeps showing up as -5. Here's what I've got.

$rec{'Total'} = int(($rec{'Field1'} - $rec{'Field2'}));

I also tried adding the total to itself, then multiplying by 2, but then it just gives me -10! Aargh, it works well on a calculator!! :(

I didn't think my math was that bad, but I'm stumped. Any ideas? Thanks.
Quote Reply
Re: [matto] help with calculations In reply to
remove the int function....

Like

Quote:

my $Total = $rec{'Field1'} - $rec{'Field2'};


Then print the Total variable as:

<%Total%>

After you've defined in the appopriate subroutine in the site_html_templates.pl.

Good luck.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] help with calculations In reply to
Hey Stealth, thanks for the reply. I just tried and it still shows the - sign. Any other ideas? Thanks.
Quote Reply
Re: [matto] help with calculations In reply to
Uh...where are you defining the variable? Because the above codes DO work in the sub site_html_link routine. Makes a HUGE difference where you define variables.

Best of luck and goodbye.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] help with calculations In reply to
Oh, sorry, I'm actually doing it for the detail page. I figured it would work the same. The value prints fine, it's just the (-) sign shows up. Is it possible to do it for that page? Thank you for your help.
Quote Reply
Re: [matto] help with calculations In reply to
The way you had it originally was fine - you didn't have to remove int()

Make sure the variables contain the values you think they do. that is likely to be the problem.
Quote Reply
Re: [Paul] help with calculations In reply to
Hey Paul. Yeah, it was working fine, like I originally had it, but the problem was with the - sign. Both fields are positive numbers, so I'm not sure why this is happening. I mean the number that is being printed is correct (calculations are right) but I just can't figure out why it's showing the - sign in front of the new value...when it should be a positive number. Any other ideas would be cool. Thanks to both of you for your help.
Quote Reply
Re: [matto] help with calculations In reply to
If you are certain they are both positive numbers then I can't see any reason why it would add the minus sign.

What does the following print?

Code:
my $ans = 10 - 2;
print $ans;

Last edited by:

Paul: Nov 17, 2002, 5:28 PM
Quote Reply
Re: [Paul] help with calculations In reply to
The fact that two numbers are positive does not imply that their difference is positive as well.

Anyway:
Code:
$rec{'Total'} = abs int($rec{Field1} - $rec{Field2});

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] help with calculations In reply to
If two numbers are positive surely the difference must be too?
Quote Reply
Re: [Paul] help with calculations In reply to
Paul, please!

a = 3
b = 8

a-b=?

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] help with calculations In reply to
Hmm I meant the left side being larger than the right side as was in the case above. It has to be positive which is why I don't understand how 10 - 5 = -5
Quote Reply
Re: [Paul] help with calculations In reply to
Hehe, good point Yogi! But yeah, the first number is always greater than the second. So, I'm still confused. I'm going to just try it with predetermined numbers, like Pauls sample (without using the fields) and see if that works. I'll let you guys know how it turns out. Thanks.