Swift : Convert (cast) Float to Int or Int32 value




Problem :

How to convert float variable value to type integer in Swift programming language?

Solution :

Use the Int() function. For examples :

 let x : Float = 8.0
 var i : Int = Int(x)

or

 let x : Float = 8.0
 var i : Int32 = Int32(x)

Reference :

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/SwiftProgrammingLanguage/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-ID338

  See also : Swift : Convert (cast) Float to String





By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement