开发者

Sum NSArray of NSDecimalNumbers

开发者 https://www.devze.com 2023-03-17 15:26 出处:网络
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?

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"];


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消