Turn a NSDecimalNumber negative
Similar to the answer by D.Shawley, but extension and swift style.
extension NSDecimalNumber {
/* Answers (aDecimal x -1) */
func decimalNumberByNegating() -> NSDecimalNumber {
return self.decimalNumberByMultiplyingBy(NSDecimalNumber(mantissa: 1, exponent: 0, isNegative: true));
}
}
You could use NSDecimalNumber>>decimalNumberWithMantissa:exponent:isNegative to generate -1 more concisely.
/* Answers (aDecimal x -1) */
NSDecimalNumber* negate(NSDecimalNumber *aDecimal) {
return [aDecimal decimalNumberByMultiplyingBy:
[NSDecimalNumber decimalNumberWithMantissa: 1
exponent: 0
isNegative: YES]];
}