I'm doing some currency math and thus using the Decimal
type. I want to test if a decimal number is an integer number (i.e a positive or negative whole number not the Int
type). There's no modulus operator for Decimal
in Swift, otherwise I'd just use that.
My understanding of decimal types is that they represent numbers as integer * exponent
. Thus any integer number will have an exponent ≥ 0, and any number with a fraction component will have an exponent < 0.
Am I correct in my understanding, and this sample would be accurate?
if someDecimal.exponent >= 0 {
print("Integer")
}
else {
print("Fraction")
}