Convert Float to Int in Swift

前端 未结 13 1926
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  萌比男神i
    2020-11-28 23:32

    You can get an integer representation of your float by passing the float into the Integer initializer method.

    Example:

    Int(myFloat)
    

    Keep in mind, that any numbers after the decimal point will be loss. Meaning, 3.9 is an Int of 3 and 8.99999 is an integer of 8.

提交回复
热议问题