I am using the following code to work out the percentages of two numbers, could some on please help me remove the decimal numbers off pcntNo
and pcntYes
//Work out percentages
int yes = [currentYes intValue];
int no = [currentNo intValue];
int total = yes + no;
int pcntYes = (yes / total) * 100;
int pcntNo = (no / total) * 100;
It always returns 0
Also i want it with no decimal pl开发者_StackOverflow中文版aces if that is possible
Thanks
What's "decimal numbers"? If you mean fractional part, you won't have those - your percentages are stored as integers. By the way, you want slightly different arithmetics:
int pcntYes = (yes * 100) / total;
int pcntNo = (no *100) / total;
Otherwise, integer division will yield only zeros.
Use the floorf function from math.h:
int pcntYes = floorf((yes / total) * 100);
精彩评论