Checking if a double value is an integer - Swift

后端 未结 12 2047
梦如初夏
梦如初夏 2020-12-14 14:18

I need to check if a double-defined variable is convertible to Int without losing its value. This doesn\'t work because they are of different types:

if self.         


        
12条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 14:54

    Using mod (%) won't work anymore.

    You can now use:

    let dbl = 2.0
    let isInteger = dbl.truncatingRemainder(dividingBy: 1.0) == 0.0
    

提交回复
热议问题