获取两个整数的百分比
我几乎已经完成了获得两个整数的百分比的一半,我只需要最后一个障碍的帮助,这是我的编码,远远低于它的工作原理,但有时它就像 yes = 50 和 no = 20%,而它应该是 50/50 。
int yes;
int no;
//Work out percentages
if ([VotedAnswer.text isEqualToString:@"No"]){
yes = [currentYes intValue];
no = [currentNo intValue] + 1;
}else{
yes = [currentYes intValue] + 1;
no = [currentNo intValue];
}
int total = yes + no + 1;
int pcntYes = (yes *100) / total;
int pcntNo = (no *100) / total;
float barNo = pcntNo / 100.0f;
float barYes = pcntYes / 100.0f;
//Set percent labels
yesPercent.text = [NSString stringWithFormat:@"%d%%", pcntYes];
noPercent.text = [NSString stringWithFormat:@"%d%%", pcntNo];
//Set Percent Bars
YesProgress.progress = barYes;
NoProgress.progress = barNo;
I am almost half way there to getting the percentage of two integers i just need help with the final hurdle, here is my coding so far below it works but sometimes its like yes = 50 and no = 20% when it should be 50/50.
int yes;
int no;
//Work out percentages
if ([VotedAnswer.text isEqualToString:@"No"]){
yes = [currentYes intValue];
no = [currentNo intValue] + 1;
}else{
yes = [currentYes intValue] + 1;
no = [currentNo intValue];
}
int total = yes + no + 1;
int pcntYes = (yes *100) / total;
int pcntNo = (no *100) / total;
float barNo = pcntNo / 100.0f;
float barYes = pcntYes / 100.0f;
//Set percent labels
yesPercent.text = [NSString stringWithFormat:@"%d%%", pcntYes];
noPercent.text = [NSString stringWithFormat:@"%d%%", pcntNo];
//Set Percent Bars
YesProgress.progress = barYes;
NoProgress.progress = barNo;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该将 1 添加到总数中。投票总数就是赞成+反对。
哦,不要忘记更新 currentYes 和 currentNo,您还没有显示执行此操作的代码。
You should not be adding 1 to total. The total number of votes cast is simply yes + no.
Oh and don't forget to update currentYes and currentNo you haven't shown code that is doing that.
这对我有用
It works for me