How do I add NSDecimalNumbers?

OK this may be the dumb question of the day, but supposing I have a class with :

NSDecimalNumber *numOne   = [NSDecimalNumber numberWithFloat:1.0];
NSDecimalNumber *numTwo   = [NSDecimalNumber numberWithFloat:2.0];
NSDecimalNumber *numThree = [NSDecimalNumber numberWithFloat:3.0];

Why can’t I have a function that adds those numbers:

- (NSDecimalNumber *)addThem {
    return (self.numOne + self.numTwo + self.numThree);
}

I apologize in advance for being an idiot, and thanks!

Adding multiple NSDecimalNumbers

What’s the best practice for doing this: I have an array of NSDecimalNumbers and I need to add them all together. I was going to set up a base number and then loop and add each subsequent number to t

How do I add observer to UIButton?

I want be signaled when user will touch button (UIControlEventTouchUpInside). How do I add observer to UIButton?

How do I add a horizontal scrollbar in a listview?

How do I add a horizontal scrollbar in a listview?

How do I add custom buttons to UIAlertView?

How do I add custom buttons to UIAlertView ??

intellij – How do I add JProgressbar to the swing palette?

How do I add JProgressbar to the swing palette?

How do I add Validation?

I need to add a validation to this code in VB. Also, Display a message box if the new balance would be a negative number. If there is not enough money to cover a check, do not deduct the check amount.

How do I add custom fonts? [duplicate]

This question already has an answer here: How to add some non-standard font to a website? 20 answers How do I install a custom font on an html site? For example, I was working on a wedding web

how do i add items in combobox?

i am having a problem. How do i add items in comboBox? I already tried this code: comboBox1.Items.Add(–Dates–); comboBox1.SelectedIndex = 0; but it is cannot add the items in comboBox when i run

How do I add a UITtoolbar to my main view.nib?

How do I add using the InterfaceBuilder?

How do I add R drawable android?

Basicly my question is that how do I add a drawable resource to the R.java file in android eclipse so I can use it with this code: ByteArrayOutputStream stream = new ByteArrayOutputStream(); Bitmap b

Answers

[[numOne decimalNumberByAdding:numTwo] decimalNumberByAdding:numThree]

You can’t do what you want becuase neither C nor Objective C have operator overloading. Instead you have to write:

- (NSDecimalNumber *)addThem {
    return [self.numOne decimalNumberByAdding:
        [self.numTwo decimalNumberByAdding:self.numThree]];
}

If you’re willing to play dirty with Objective-C++ (rename your source to .mm), then you could write:

NSDecimalNumber *operator + (NSDecimalNumber *a, NSDecimalNumber *b) {
    return [a decimalNumberByAdding:b];
}

Now you can write:

- (NSDecimalNumber *)addThem {
    return self.numOne + self.numTwo + self.numThree;
}

Go C++!

See NSDecimalAdd function (as well as NSDecimalMultiply, NSDecimalDivide, NSDecimalSubtract).

You can:

- (NSDecimalNumber *)addThem {
    return [[self.numOne decimalNumberByAdding:numTwo] decimalNumberByAdding:numThree];
}

The problem with your example is that what self.numOne really is at a bit level is a pointer to an object. So your function would return some random memory location, not the sum.

If Objective-C supported C++-style operator overloading, someone could define + when applied to two NSDecimalNumber objects as an alias to decimalNumberByAdding:. But it doesn’t.

NSDecimalNumber is an Objective C class which, when instantiated, produces an object which contains a number. You access the object (and objects in general) through methods only. Objective C doesn’t have a way to directly express arithmetic against objects, so you need to make one of three calls:

  • -[NSDecimalNumber doubleValue], which extracts the numbers from your objects before adding the numbers. link
  • -[NSDecimalNumber decimalNumberByAdding], which creates an additional object, which may be more than you want. link
  • NSDecimalAdd, which, again, creates an additional object, which may be more than you want. link