【问题标题】:Get Percentage from two integers从两个整数中获取百分比
【发布时间】:2011-08-21 05:28:57
【问题描述】:

我几乎是获得两个整数的百分比的一半,我只需要帮助解决最后一个障碍,这是我的编码到目前为止低于它的工作,但有时它就像是 = 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;

【问题讨论】:

    标签: iphone objective-c math int


    【解决方案1】:

    您不应该在总数中加 1。总票数就是yes + no。 哦,别忘了更新 currentYes 和 currentNo 你还没有显示这样做的代码。

    【讨论】:

      【解决方案2】:

      对我有用

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

      【讨论】:

        猜你喜欢
        • 2012-01-31
        • 1970-01-01
        • 1970-01-01
        • 2021-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-28
        • 1970-01-01
        相关资源
        最近更新 更多