想要将一个小数取整:
超过0.5以上的,整数加1
小于0.5的,不算
或者是超过指定比例的,比如超过0.8以上的,整数加1
比如:
5.81967213114754
取整后是5+1=6
swift float round to int
Convert Float to Int in Swift – Stack Overflow
How to round a Double to the nearest Int in swift? – Stack Overflow
后来是去用round即可:
let maxCellNumPerRowFloat:Float = Float(self.view.bounds.width – self.flowLayout.sectionInset.left) / Float(self.flowLayout.itemSize.width + self.flowLayout.sectionInset.left) // print("maxCellNumPerRowFloat=\(maxCellNumPerRowFloat)") //5.81967 let roundedMaxCellPerRowFloat = round(maxCellNumPerRowFloat) // print("roundedMaxCellPerRowFloat=\(roundedMaxCellPerRowFloat)") //6.0 let maxCellNumPerRow:Int = Int(roundedMaxCellPerRowFloat) // print("maxCellNumPerRow=\(maxCellNumPerRow)") //6 |
转载请注明:在路上 » [已解决]swift将浮点数的小数点round后取整数