Re: Percentage between two values

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: George Inacio (jginacio_at_hotmail.com)
Date: 10/28/04


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/


Relevant Pages

  • Re: HP World Magazine: For Business Continuity...the answer may be OpenVMS
    ... I happened to know the approximate cost to manufacture DS15 when it was ... There isn't all that much profit in the sale of these systems. ... The only way to profitably sell Alphas much cheaper would be to cut the ...
    (comp.os.vms)
  • Re: New to all this - need some advice and answers
    ... text normally associated with a for sale post (something like "1976 ... Cost relative to renting? ... can anyone recommend more aircraft for sale web sites? ... As a non-pilot the insurance is essentially for training. ...
    (rec.aviation.owning)
  • Re: Pivot table sum issue
    ... What happens when you try to put both Cost and Sale in the Data area? ... Canada 4 9 ... Foreign 9 21 ...
    (microsoft.public.excel.misc)
  • Re: Fabric Club membership?
    ... I think that they have reduced the cost of the plan which is ... Last time I was in the store ... visit - I had some fabric which was sale priced while the notions were ... Cynthia even sent me a pack of 8 half yard cuts one month of fabrics ...
    (rec.crafts.textiles.quilting)
  • RE: FIFO: Share calculation in VBA?
    ... Start from the date that you are trying to cost. ... 'Balance Prior Bin ... If Balance <= ActiveCell.Value Then ... > of that sale would be the exact price of the corresponding buy. ...
    (microsoft.public.excel.programming)