-1

I'm using this code to invert an NSDecimalNumber for easier arithmetic:

    self.thisTransaction.amount = [NSDecimalNumber decimalNumberWithString:self.amountField.text locale:NSLocale.currentLocale];

    NSLog(@"1 thisTransaction.amount is %@",self.thisTransaction.amount);

    // Inverts the transaction amount

    // This code negates the number, but chops off the part on the right side of the decimal

    self.thisTransaction.amount = [NSDecimalNumber decimalNumberWithMantissa:[self.thisTransaction.amount longLongValue] exponent:0 isNegative:YES];

    NSLog(@"2 thisTransaction.amount is %@",self.thisTransaction.amount);

Well, I thought the arithmetic would be easier, anyway. However, as revealed in the console readout below, the code negates the number, but truncates the number at the decimal point and returns a whole integer instead of a decimal:

2015-05-27 10:07:30.767 XXXXX[11835:10266658] 1 thisTransaction.amount is 25.49
2015-05-27 10:07:39.735 XXXXX[11835:10266658] 2 thisTransaction.amount is -25

I've looked at quite a few posts on SO, but they seem to go around in circles. I'd sure appreciate some guidance.

EDIT:

Pursuant to @rmaddy's suggestion below, I changed the code as follows:

    self.thisTransaction.amount = [NSDecimalNumber decimalNumberWithString:self.amountField.text locale:NSLocale.currentLocale];

    NSLog(@"1 thisTransaction.amount is %@",self.thisTransaction.amount);

    // Inverts the transaction amount

    NSDecimalNumber * invertedMultiplier = [NSDecimalNumber decimalNumberWithString:@"-1"];

    self.thisTransaction.amount = [self.thisTransaction.amount decimalNumberByMultiplyingBy:invertedMultiplier];



    NSLog(@"2 thisTransaction.amount is %@",self.thisTransaction.amount);

This seems to work fine.

rattletrap99
  • 1,469
  • 2
  • 17
  • 36
  • Thanks, and please see edit above. – rattletrap99 May 27 '15 at 22:38
  • The accepted answer was amended after I made the edit. And in fact, i used a slightly different approach than the amended answer. Apparently both strategies will work, but the fix was clearly suggested by @rmaddy's original answer. Thanks! – rattletrap99 May 27 '15 at 22:44

1 Answers1

3

If you want to negate an NSDecimalNumber you can simply do:

NSDecimalNumber *originalNumber = ... // the original decimal number

NSDecimalNumber *negatedNumber = [originalNumber decimalNumberByMultiplyingBy:[NSDecimalNumber numberWithInt:-1]];

And for fun, here's another way:

NSDecimalNumber *anotherNegate = [[NSDecimalNumber zero] decimalNumberBySubtracting:originalNumber];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • That almost worked, but I had to rephrase it as shown in the edit above. Thanks! – rattletrap99 May 27 '15 at 22:33
  • Oops. Fixed the use of the `-1`. – rmaddy May 27 '15 at 22:34
  • I stumbled over an alternate fix as shown above. But your's looks more efficient. – rattletrap99 May 27 '15 at 22:37
  • Thanks for another option. I have to admit that I'm having a hell of a time coming to grips with the multi-stepped two-way conversions needed to deal with accepting, manipulating, and displaying numbers stored in Core Data. I can use all the help I can get. Thanks again! – rattletrap99 May 27 '15 at 22:50