Created
December 28, 2011 17:37
-
-
Save MrRooni/1528822 to your computer and use it in GitHub Desktop.
How to create a negative zero
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Call this method with [NSDecimalNumber zero] to get negative zero, or NaN. | |
- (NSDecimalNumber *)negativeAmount:(NSDecimalNumber *)anAmount | |
{ | |
if (anAmount == nil) { | |
return [NSDecimalNumber zero]; | |
} | |
NSDecimal decimalAmount = [anAmount decimalValue]; | |
decimalAmount._isNegative = 1; | |
return [NSDecimalNumber decimalNumberWithDecimal:decimalAmount]; | |
} | |
// Here's a version that handles that situation properly: | |
- (NSDecimalNumber *)negativeAmount:(NSDecimalNumber *)anAmount | |
{ | |
if (anAmount == nil || [anAmount isZero]) { | |
return [NSDecimalNumber zero]; | |
} | |
NSDecimal decimalAmount = [anAmount decimalValue]; | |
decimalAmount._isNegative = 1; | |
return [NSDecimalNumber decimalNumberWithDecimal:decimalAmount]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The isZero method is from a category we have on NSDecimalNumber.