Skip to content

Instantly share code, notes, and snippets.

@MrRooni
Created December 28, 2011 17:37
Show Gist options
  • Select an option

  • Save MrRooni/1528822 to your computer and use it in GitHub Desktop.

Select an option

Save MrRooni/1528822 to your computer and use it in GitHub Desktop.

Revisions

  1. Michael Fey created this gist Dec 28, 2011.
    25 changes: 25 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    // 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];
    }