开发者

work out percentage

开发者 https://www.devze.com 2023-03-03 12:05 出处:网络
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

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消