tags:

views:

353

answers:

4

I take This Week's Revenue and Last Week's Revenue values from the server and save them as: RevenueThisWeek = objRevenueThisWeek("REVENUE") RevenueLastWeek = objRevenueLastWeek("REVENUE") Then, I calculate percentage of them as: RevenuePercent = RevenueThisWeek - RevenueLastWeek RevenuePercent = RevenuePercent / RevenueLastWeek RevenuePercent = RevenuePercent * 100

Problem here is, if both RevenueThisWeek and RevenueLastWeek are 0, I receive the following error message.

Microsoft VBScript runtime error '800a0006'

Overflow


edit

Forgot to mention. I created an if statement as

If (RevenueThisWeek AND RevenueLastWeek) = "0" Then
Else
RevenuePercent = RevenueThisWeek - RevenueLastWeek
RevenuePercent = RevenuePercent / RevenueLastWeek
RevenuePercent = RevenuePercent * 100 
End If

But this gave me results like:

      Last Week   This Week   % Change 
Revenue  $49.99     $72.97      0%
+3  A: 

Divide by Zero

Dana Holt
+1  A: 

Well, you are asking ASP to perform the following calculation:

RevenuePercent = RevenueThisWeek - RevenueLastWeek
RevenuePercent = RevenuePercent / RevenueLastWeek
RevenuePercent = RevenuePercent * 100

or, given the figure of 0 for RevenueThisWeek and RevenueLastWeek :

RevenuePercent = 0 - 0
RevenuePercent = 0/0
RevenuePercent = 0*100

the problematic line is actually the middle calculation:

RevenuePercent = 0/0

as you are asking a computer to divide by 0 - and that's not possible. You'll need to add an IF statement along the lines of the following pseudocode:

IF RevenueLastWeek>0 THEN ...perform calculation.. ELSE RevenuePercent=0 ENDIF
Richy C.
+2  A: 
RevenuePercent = RevenueThisWeek - RevenueLastWeek
If RevenueLastWeek >= .01 Then '1 cent minimum. >0 can get messy.'
   RevenuePercent = RevenuePercent / RevenueLastWeek
Else
   RevenuePercent = 0
End If
RevenuePercent = RevenuePercent * 100

Why the >.01?

VBScript doesn't have a decimal type and defaults to floats which means you could end up with a value of .0000004505 or something ugly which will still result in an overflow. If you deal with tenths of a cent change it to

If RevenueLastWeek >= .001 Then
Spencer Ruport
when used 0, sometimes it worked, sometimes not. Thank you!
A: 

Essentially this is what you are doing:

RevenuePercent = (objRevenueThisWeek("REVENUE") - objRevenueLastWeek("REVENUE"))*100/objRevenueLastWeek("REVENUE")

If objRevenueLastWeek("REVENUE") = 0 then you are dividing by 0 and hence the overflow. You may need a conditional to account for this case.

--

HiteshP