Testing if a Decimal is a whole number in Swift

后端 未结 3 1844
慢半拍i
慢半拍i 2020-12-19 14:15

Using Swift 3.

I am finding a lot of strange solutions online for checking if a Decimal object is a whole number. Everything feels far more complicated then it needs

3条回答
  •  一个人的身影
    2020-12-19 14:22

    I'm not sure can works in all cases but that maybe a more coincise option

    extension Decimal {
    
        static var decimalSeparator: String { return NumberFormatter().decimalSeparator }
    
        var isFraction: Bool {
            return self.description.contains(Decimal.decimalSeparator)
        }
    
        var isWhole: Bool {
            return !isFraction
        }
    
    }
    

提交回复
热议问题