Skip to content

Instantly share code, notes, and snippets.

@MrRooni
Created December 28, 2011 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrRooni/1528822 to your computer and use it in GitHub Desktop.
Save MrRooni/1528822 to your computer and use it in GitHub Desktop.
How to create a negative zero
// 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];
}
@MrRooni
Copy link
Author

MrRooni commented Dec 28, 2011

The isZero method is from a category we have on NSDecimalNumber.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment