关于objective C:为什么将 NSNumber 除以浮点数会将结果四舍五入到小数点后 1 位?

why does division of NSNumber by float gives result rounded to 1 decimal place?

见以下代码:

1
2
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemFreeSize];
float totalSpaceInMB = (([fileSystemSizeInBytes longLongValue] /1024.0)/1024.0);

这里 totalSpaceInMB 总是四舍五入到小数点后 1 位。

例如,如果 fileSystemSizeInBytes = 13261987840,那么 totalSpaceInMB = 12647.6

我想要 4 位小数,totalSpaceInMB = 12647.6172。为什么会这样?


1
2
3
4
5
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemFreeSize];

double totalSpaceInMB = (([fileSystemSizeInBytes longLongValue] /1024.0)/1024.0);

NSLog(@"%.4f",totalSpaceInMB);

希望这会有所帮助:)


最好尝试一下:

1
2
3
NSNumber *fileSystemSizeInBytes = [dictionary objectForKey: NSFileSystemFreeSize];
double totalSpaceInMB = (([fileSystemSizeInBytes longLongValue] /1024.0)/1024.0);
NSLog(@"%.4lf",totalSpaceInMB);