Re: Percentage between two values
From: George Inacio (jginacio_at_hotmail.com)
Date: 10/28/04
- Next message: Bob Cummings: "memory error"
- Previous message: Randy Birch: "Re: Percentage between two values"
- In reply to: George Inacio: "Percentage between two values"
- Next in thread: Charlie: "RE: Percentage between two values"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 28 Oct 2004 05:30:45 +0200
Thanks Everybody!
That is a great help because I was real lost.
Regards,
George
There is no such percentage. 0 can be multiplied infinitely and still be
less than 110. That is why division by zero is always an error.
To work around this, your code can test for when cost = 0, and you can set
to ProfitPercent to a special value in that case. But I have no idea what
value that would be. The percentage is infinite.
-- Jonathan Wood SoftCircuits http://www.softcircuits.com Available for consulting: http://www.softcircuits.com/jwood/resume.htm if cost is 0 then profit percentage is undefiined if percentage is based on cost but if profit percentage is based on sales price, then that will be 100% profit is "usually" expressed in percent of sales in most businesses I've worked for --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.781 / Virus Database: 527 - Release Date: 10/21/2004 The formula is : profit = ((sale - cost) / Abs(cost)) * 100 But since your cost is 0, you'll get a divide by 0 error if you attempt to use this formula - or any formula where cost is the denominator - when 0. You can wrap the code in a conditional test, if cost > 0 then .... end if but I recommend writing it as a function: private function ProfitPercent(nCost as single, nSale as Single) as long if nCost = 0 then ProfitPercent = 0 exit function end if ProfitPercent = ((sale - cost) / Abs(cost)) * 100 end function This lets you define all the appropritate tests for valid cost and sale values in one place, and call the routine such as: Cost = 100 Sale = 110 p = ProfitPercent(Cost, Sale) You could even code the routine to return a known value for your testing, eg ... private function ProfitPercent(nCost as single, nSale as Single) as long if nCost = 0 then ProfitPercent = -9999 '<<< exit function end if ProfitPercent = ((sale - cost) / Abs(cost)) * 100 end function Cost = 100 Sale = 110 p = ProfitPercent(Cost, Sale) if p <> -9999 then print "the profit is " p endif -- Randy Birch MS MVP Visual Basic http://vbnet.mvps.org/
- Next message: Bob Cummings: "memory error"
- Previous message: Randy Birch: "Re: Percentage between two values"
- In reply to: George Inacio: "Percentage between two values"
- Next in thread: Charlie: "RE: Percentage between two values"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|