Testing if a Decimal is a whole number in Swift

后端 未结 3 1835
慢半拍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:17

    Thanks for the comments! Here is what I am using now.

    extension Decimal {
        var isWholeNumber: Bool { 
            return self.isZero || (self.isNormal && self.exponent >= 0) 
        }
    }
    

提交回复
热议问题