Convert Float to Int in Swift

前端 未结 13 1922
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 23:16

I want to convert a Float to an Int in Swift. Basic casting like this does not work because these types are not primitives, unlike float

13条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 23:27

    Like this:

    var float:Float = 2.2 // 2.2
    var integer:Int = Int(float) // 2 .. will always round down.  3.9 will be 3
    var anotherFloat: Float = Float(integer) // 2.0
    

提交回复
热议问题