Convert Float to Int in Swift

前端 未结 13 1955
没有蜡笔的小新
没有蜡笔的小新 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:41

    Just use type casting

     var floatValue:Float = 5.4
     var integerValue:Int = Int(floatValue)
    
     println("IntegerValue = \(integerValue)")
    

    it will show roundoff value eg: IntegerValue = 5 means the decimal point will be loss

提交回复
热议问题