也许这是一个愚蠢的问题,但我真的无法理解。由于某种原因,我的程序中的浮点值在赋值语句期间被四舍五入。
这是代码:
Float64 time,test;
for( Shot *sh in _arrayOfShots)
{
time = sh.bFrameTime;
// right here time variable gets value rounded up to 2 decimal places
// for example: if sh.bFrameTime = 81.919998 => time = 81.92
NSNumber *num = [NSNumber numberWithFloat:time];
test = [num floatValue];
[edgeTime addObject:num];
}
// this is my Shot object structure
@interface Shot : NSObject
{
int bFrame;
int eFrame;
Float64 bFrameTime;
}
如果有人知道如何处理这个问题,请给我提示!
谢谢!
最佳答案
81.919998 => time = 81.92
不是“四舍五入到小数点后两位”。两者之差为 0.000002,即幅度变化为 0.0000000244,即大约 8 位小数。如果您的源值是单精度 float ,那么它的精度只有 7 位小数。 (但是您没有显示该声明,所以我们无法判断。)
差异也可能与显示两个值的方式有关,因为 NSLog 会进行少量舍入。
关于objective-c - Objective-C 中如何防止 float 四舍五入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13077172/