我几乎有一半的时间来获得两个整数的百分比我只需要最后的障碍帮助,这里是我的编码到目前为止它的工作,但有时它像是= 50和没有= 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;
答案 0 :(得分:3)
你不应该总共加1。投票的总票数仅为是+否。 哦,不要忘记更新currentYes和currentNo你没有显示正在执行此操作的代码。
答案 1 :(得分:0)
对我有用
int yes = [cell.yesAnswer.text intValue] ;
int no = [cell.noAnswer.text 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;
cell. yesProcent.text = [NSString stringWithFormat:@"%d%%", pcntYes];
cell. noProcent.text = [NSString stringWithFormat:@"%d%%", pcntNo];