最新消息:20210917 已从crifan.com换到crifan.org

[已解决]swift将浮点数的小数点round后取整数

Swift crifan 4646浏览 0评论

想要将一个小数取整:

超过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后取整数

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
101 queries in 0.189 seconds, using 23.30MB memory