Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Percentage between two values
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



Report this thread to moderator Post Follow-up to this message
Old Post
George Inacio
10-28-04 08:55 AM


Re: Percentage between two values
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

"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
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Jonathan Wood
10-28-04 08:55 AM


Re: Percentage between two values
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

"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



Report this thread to moderator Post Follow-up to this message
Old Post
Hal Rosser
10-28-04 08:55 AM


Re: Percentage between two values
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= 110.
:
: Can you help me please?
:
: Thanks in advance!
:
: Regards,
: George
:
:


Report this thread to moderator Post Follow-up to this message
Old Post
Randy Birch
10-28-04 08:55 AM


Re: Percentage between two values
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/




Report this thread to moderator Post Follow-up to this message
Old Post
George Inacio
10-28-04 08:55 AM


Re: Percentage between two values
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


"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
> :
> :
>
>

Report this thread to moderator Post Follow-up to this message
Old Post
Charlie
10-28-04 01:55 PM


Re: Percentage between two values
Charlie,

> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Jonathan Wood
10-28-04 08:55 PM


Re: Percentage between two values
> > 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 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


Report this thread to moderator Post Follow-up to this message
Old Post
Rick Rothstein
10-28-04 08:55 PM


Re: Percentage between two values
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Jim Edgar
10-28-04 08:55 PM


Re: Percentage between two values
"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"


Report this thread to moderator Post Follow-up to this message
Old Post
Bob Butler
10-28-04 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Pages (4): [1] 2 3 4 »
Search this forum -> 
Post New Thread

Visual Basic archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:26 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.