伙计们,我的浮动量很少,我想将其四舍五入为第一个而不是零的值。
例如:

float toRound = 0.000002125231553;
toRound = //Operations//;

toRound == 0.000002;


你有什么想法吗?

最佳答案

也许不是最好的方法。但是它可以满足您的要求;)

float s = 0.000322333123;
BOOL exit = 0;
NSString *x = [NSString stringWithFormat:@"%f", s];
for (int i = 0; i <= [x length]; i++) {
NSString *t = [x substringToIndex:i];

if ([t floatValue] == 0 || exit == 1) {
    ;;
}
else {
    exit = 1;
    s = [t floatValue];
}
}

NSLog(@"round: %f", s);


日志:轮:0.0003

关于objective-c - 四舍五入到第一个非零值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10362559/

10-12 00:18
查看更多