How to convert a floating point value greater than Int.max to Int in Swift How to convert a floating point value greater than Int.max to Int in Swift ios ios

How to convert a floating point value greater than Int.max to Int in Swift


Int64 is large enough to hold a time interval of some millionyears measured in milliseconds:

let milliSeconds = Int64(someDate.timeIntervalSince1970 * 1000)let milliSecondsString = String(milliSeconds)


Int64 is enough to hold the value you needed

let floatingPointValue = NSDate().timeIntervalSince1970 * 1000let intValue = Int64(floatingPointValue)


The crucial part is to use Int64 instead of Int on 32-bit platforms. It will allow you to use 8 bytes of memory (from −9223372036854775808 to +9223372036854775807)

Int64(NSDate().timeIntervalSince1970 * 1000)