Code Comments
Programming Forum and web based access to our favorite programming groups.Hello Everyone. I need to find the Profit percentage using the Cost and Sale. I use this formula to find the percentage; Cost = 100 Sale = 110 ProfitPercent = (Sale * 100 / Cost) - 100 Works fine but if the Cost = 0 and Sale= 110 I get in troubles. I have no idea how to calculate a percentage from Cost=0 with a Sale= 110. Can you help me please? Thanks in advance! Regards, George
Post Follow-up to this messageThere 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 "George Inacio" <jginacio@hotmail.com> wrote in message news:clpjdk$15d$1@ctb-nnrp2.saix.net... > Hello Everyone. > > I need to find the Profit percentage using the Cost and Sale. > I use this formula to find the percentage; > > Cost = 100 > Sale = 110 > ProfitPercent = (Sale * 100 / Cost) - 100 > > Works fine but if the Cost = 0 and Sale= 110 I get in troubles. > I have no idea how to calculate a percentage from Cost=0 with a Sale= 110. > > Can you help me please? > > Thanks in advance! > > Regards, > George > >
Post Follow-up to this messageif 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 "George Inacio" <jginacio@hotmail.com> wrote in message news:clpjdk$15d$1@ctb-nnrp2.saix.net... > Hello Everyone. > > I need to find the Profit percentage using the Cost and Sale. > I use this formula to find the percentage; > > Cost = 100 > Sale = 110 > ProfitPercent = (Sale * 100 / Cost) - 100 > > Works fine but if the Cost = 0 and Sale= 110 I get in troubles. > I have no idea how to calculate a percentage from Cost=0 with a Sale= 110. > > Can you help me please? > > Thanks in advance! > > Regards, > George > > --- 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
Post Follow-up to this messageThe 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/ "George Inacio" <jginacio@hotmail.com> wrote in message news:clpjdk$15d$1@ctb-nnrp2.saix.net... : Hello Everyone. : : I need to find the Profit percentage using the Cost and Sale. : I use this formula to find the percentage; : : Cost = 100 : Sale = 110 : ProfitPercent = (Sale * 100 / Cost) - 100 : : Works fine but if the Cost = 0 and Sale= 110 I get in troubles. : I have no idea how to calculate a percentage from Cost=0 with a Sale= 110. : : Can you help me please? : : Thanks in advance! : : Regards, : George : :
Post Follow-up to this messageThanks 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/
Post Follow-up to this messageFYI, the outer parentheses are redundant and can make for more difficult reading. As long as you know your arithmetic hierarchy you can save some typing. Division and multiplication are of the same precedence and will be performed left-to-right (i.e. order doesn't matter.) The compacted formula then is: profit = (sale - cost) / Abs(cost) * 100 "Randy Birch" wrote: > 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/ > > > "George Inacio" <jginacio@hotmail.com> wrote in message > news:clpjdk$15d$1@ctb-nnrp2.saix.net... > : Hello Everyone. > : > : I need to find the Profit percentage using the Cost and Sale. > : I use this formula to find the percentage; > : > : Cost = 100 > : Sale = 110 > : ProfitPercent = (Sale * 100 / Cost) - 100 > : > : Works fine but if the Cost = 0 and Sale= 110 I get in troubles. > : I have no idea how to calculate a percentage from Cost=0 with a Sale= 11 0. > : > : Can you help me please? > : > : Thanks in advance! > : > : Regards, > : George > : > : > >
Post Follow-up to this messageCharlie, > FYI, the outer parentheses are redundant and can make for more difficult > reading. As long as you know your arithmetic hierarchy you can save some > typing. Division and multiplication are of the same precedence and will be > performed left-to-right (i.e. order doesn't matter.) The compacted formula > then is: > > profit = (sale - cost) / Abs(cost) * 100 I agree with you that the additional parentheses are not required. However, I agree with Randy about which is easier to read. I almost always include the additonal parentheses to make it obvious (at the cost of a little extra typing) when reading the code. -- Jonathan Wood SoftCircuits http://www.softcircuits.com Available for consulting: http://www.softcircuits.com/jwood/resume.htm
Post Follow-up to this message> > FYI, the outer parentheses are redundant and can make for more difficult some will > be > formula > > I agree with you that the additional parentheses are not required. However, > I agree with Randy about which is easier to read. I almost always include > the additonal parentheses to make it obvious (at the cost of a little extra > typing) when reading the code. Personally, I don't like extra parentheses. I agree, some might getabout what is being multiplied by 100 given the layout. My solution would be to write the above this way... profit = 100 * (sale - cost) / Abs(cost) I find very few people get
for this layout. Rick - MVP
Post Follow-up to this message"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message news:e4rm4vQvEHA.2520@TK2MSFTNGP15.phx.gbl... > difficult > some > will > However, > include > extra > > Personally, I don't like extra parentheses. I agree, some might get >about what is being multiplied by 100 given the layout. My > solution would be to write the above this way... > > profit = 100 * (sale - cost) / Abs(cost) > > I find very few people get
for this layout. > > Rick - MVP > I'm one of the few people. Math and operator precedence was 35 years ago. IMHO I like Randy's explicit style. Jim Edgar
Post Follow-up to this message"Jim Edgar" <djedgarNOSPAM@cox.net> wrote in message news:%23sfQd7QvEHA.1292@TK2MSFTNGP10.phx.gbl > > I'm one of the few people. Math and operator precedence > was 35 years ago. IMHO I like Randy's explicit style. It was a bit less than that for me but I also prefer the extra parens since I can see immediately what the order of operations is without having to stop and think about operator precedence at all. -- Reply to the group so all can participate VB.Net... just say "No"
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.