Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum NSArray of NSDecimalNumbers

Tags:

objective-c

I know you can sum an array of NSNumbers by using the @sum.self keypath, but does that also work with NSDecimalNumbers? Will the result be accurate?

EDIT: To be more specific, here is code I know that works with NSNumber.

NSNumber *sum = [numArray valueForKeyPath:@"@sum.self"];
like image 563
Cory Imdieke Avatar asked Jul 06 '11 20:07

Cory Imdieke


1 Answers

NSDecimalNumber is a subclass of NSNumber, so it will inherit this ability.

Also, I would recommend using one of these:

NSDecimalNumber *sum = [numArray valueForKeyPath:@"@sum.floatValue"];

float sum = [[numArray valueforKeyPath:@"@sum.floatValue"] floatValue];
like image 173
PengOne Avatar answered Nov 09 '22 18:11

PengOne