Checking if a double value is an integer - Swift

后端 未结 12 2041
梦如初夏
梦如初夏 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条回答
  •  旧时难觅i
    2020-12-14 14:51

    extension FloatingPoint {
        var isWholeNumber: Bool { isNormal ? self == rounded() : isZero }
    }
    
    let double = 3.0    
    double.isWholeNumber        // true
    print(3.15.isWholeNumber)   // false
    

提交回复
热议问题