Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange problem with calculations 1

Status
Not open for further replies.

KempCGDR

Programmer
Jan 10, 2003
445
GB
Hi, part of my code has to calculate what percentage two numbers are of the whole (ie both the them). To do this, I used the same calculations which worked in school and worked in Delphi mere hours ago (I basically copied the code over). However, VB's percentages don't add up to 100.

The code:
Code:
total = introVal + extroVal
introValP = (introVal / total) * 100
extroValP = (extroVal / total) * 100

The only time this has produced the right results is when the two numbers introVal and extroVal add up to 10 (I guess it's easier on VB that way ;) ). Other times it either comes to around ~80-90% or ~105%, any thoughts on this?
 
What are the datatypes and can you give some examples.

======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
I would guess that there is some implicity type conversion going on. It is probably to do with the / (integer division) \ is a better operator to use in this context

try
Code:
total = introVal + extroVal
introValP = cdbl((introVal \ total)) * 100
extroValP = cdbl((extroVal \ total)) * 100

also worth ensuring that the introValP and extrovalP variables are defined appropriately!

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
KempCGDR,

Assuming types are double, should be like this?

total = introVal + extroVal
introValP = (introVal / total) * 100.
extroValP = (extroVal / total) * 100.
totalP = introValP + extroValP

The new variable totalP should contain 100.

vladk
 
No, no, Matt, we've been here before (thread222-737409): '/' is not integer division (Div); '\' is.
 
so, could try forcing them to doubles:

total = introVal + extroVal
introValP = (cdbl(introVal) / cdbl(total)) * 100
extroValP = (cdbl(extroVal) / cdbl(total)) * 100
 
It will almost certainly be being caused by the fact that 'total' is dimmed as a Long or an Integer. Under those circumstances none of the casting solutions here will fix the problem.

The solution would be to dim 'total' correctly...
 
They were all cast as doubles except for
Code:
total
. Don't know how I missed something so obvious, sorry.
 
>No, no, Matt, we've been here before (Thread222-737409): '/' is not integer division (Div); '\' is.


aaargh.....

I got em the wrong way round....


Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
>Don't know how I missed something so obvious

Trust me, it happens to us all at some point or another ;-)
 
Anyway, to the guys that spotted the type issue, a star for your time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top