目标C中的舍入数字到更大的值

时间:2012-11-06 07:04:25

标签: iphone objective-c

我希望将我的数字舍入到更高的价值。例如,如果我有234我想让它300和4436到5000.我已经尝试过,但我可以圆满我的价值不是百和thousants而是十几。就像我有一个7771的价值,它会缩小到7900或7800,但我想要它在8000.

int temp = lroundf([[arrayPercentage objectAtIndex:i]floatValue]);
maxPer = (((temp + 10)/10))*10;

2 个答案:

答案 0 :(得分:1)

这个怎么样:

-(void)roundUpNumber:(NSInteger) num {
    NSInteger numLength = [[NSString stringWithFormat:@"%ld",num] length];
    NSInteger newNum = ceil(num/pow(10,numLength-1)) * pow(10,numLength -1);
    NSLog(@"%ld",newNum);
}

答案 1 :(得分:1)

int num, count = 0;
    int originalNumber = 7771;

    num = originalNumber;
    while (num) {
        num = num/10;
        count ++;
    }

    int power = pow(10,(count -1));

    int firstDigit = originalNumber / power;


    int finalNumber = (firstDigit + 1)*  power;

    NSLog(@"final result : %d",finalNumber);