如何在Swift中将大于Int.max的浮点值转换为Int

时间:2022-08-24 07:30:53

I want to have the integer value of the following floating point value:

我想要具有以下浮点值的整数值:

var floatingPointValue = NSDate().timeIntervalSince1970 * 1000

I don't care if the integer value of this floating point number is actually an integer or a string.

我不在乎这个浮点数的整数值实际上是整数还是字符串。

2 个解决方案

#1


21  

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

Int64足够大,可以以毫秒为单位保持几百万年的时间间隔:

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

#2


1  

Int64 is enough to hold the value you needed

Int64足以保存您需要的值

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

#1


21  

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

Int64足够大,可以以毫秒为单位保持几百万年的时间间隔:

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

#2


1  

Int64 is enough to hold the value you needed

Int64足以保存您需要的值

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