NSMutableArray *array = [NSMutableArray arrayWithObjects:@"22,343", @"44,323",@"34,5678",@"22,725", nil];
我有这个数组,我想把这个数组的两个值相加。
像这样array[0] + array [1]
如何总结这些元素? 谢谢!!
答案 0 :(得分:0)
解决方案
NSString *strVaue1 = [array[0] stringByReplacingOccurrencesOfString:@"," withString:@""];
NSString *strVaue2 = [array[1] stringByReplacingOccurrencesOfString:@"," withString:@""];
NSUInteger arrValue1 = [strVaue1 integerValue];
NSUInteger arrValue2 = [strVaue2 integerValue];
NSUInteger sum = arrValue1 + arrValue2;
NSNumberFormatter *fmt = [[NSNumberFormatter alloc] init];
[fmt setNumberStyle:NSNumberFormatterDecimalStyle]; // to get commas (or locale equivalent)
[fmt setMaximumFractionDigits:0]; // to avoid any decimal
NSString *result = [fmt stringFromNumber:@(sum)];
NSLog(@"The result is - %@",result);
打印结果是
The result is - 66,666