这里的问题很简单。我有一个 double,我希望将其转换回 NSInteger,截断到单位位置。我该怎么做?
最佳答案
截断是一种隐式转换:
NSInteger theInteger = theDouble;
假设您没有检查值是否在 NSInteger 的范围内。如果你想这样做,你必须添加一些分支:
NSInteger theInteger = 0;
if (theDouble > NSIntegerMax) {
// ...
} else if (theDouble < NSIntegerMin) {
// ...
} else {
theInteger = theDouble;
}
关于iphone - 如何将 double 转换为 NSInteger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4671348/