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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating Percentages

Status
Not open for further replies.

kav123

Programmer
Jan 12, 2005
210
GB
I am trying to calculate the percentage value. The values for which the percentage needs to be calculated, they are coming from a database, they are coming fine.
This is what i am doing:

percent = (cCount/totalCount)*100;

Although i am getting values for cCount and totalCount, the total percentage always comes zero.
The 'percent' variable is of float type.

Looks like a simple thing, but am i missing something really basic here.
The values i am getting i.e. cCount and totalCount are both float.
 
my guess is 100 is an integer by default. (cCount/totalCount) is implicitly converted to an int before multiplying my 100 which would be 0. 0*100=0.
try this instead
Code:
float cCount = 2;
float totalCount = 10;
float one_hundred = 100;
float percent = (cCount/totalCount)*one_hundred;
or
Code:
percent = (cCount/totalCount)*100F;
the F after 100 allows the system to interpert the 100 as a float rather than the default (int) type. If the flag is not F it's something like that
M = decimal
D = double
etc.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason, the flag is definitely F.

Regards

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Both the variables cCount and totalCount need to be of type float.
 
If he only needs integer part, he can simply use
percent = 100*cCount/totalCount;
and he will get whole part
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top