关于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。为什么会这样?

  • 你如何显示数字?
  • (请记住,浮点数只有大约 6 个有效数字。如果需要更多,请使用双精度数。)
  • (并不是说将总 MB 显示到该精度水平是很有意义的。)


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);
  • 这个 NSLog 将输出输出到十进制的 4 位。但我必须根据空间MB(totalSpaceInMB / x)进一步计算。执行此操作时,将 totalSpaceInMB 作为 1 个小数点


来源:https://www.codenong.com/12406097/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9165.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?