获取两个整数的百分比

发布于 2024-11-05 23:15:46 字数 751 浏览 1 评论 0原文

我几乎已经完成了获得两个整数的百分比的一半,我只需要最后一个障碍的帮助,这是我的编码,远远低于它的工作原理,但有时它就像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的海 2024-11-12 23:15:46

您不应该将 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.

辞别 2024-11-12 23:15:46

这对我有用

 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];

It works for me

 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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文