iOS小常识 数组求和、最大,最小、平均值

xiaoxiao2021-02-28  2

-(void)testSome {     NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];          CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];          CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];          CGFloat max = [[array valueForKeyPath:@"@max.floatValue"] floatValue];          CGFloat min = [[array valueForKeyPath:@"@min.floatValue"] floatValue];          NSLog(@"%f\n%f\n%f\n%f",sum,avg,max,min);

}

输出:

2018-01-18 10:51:59.806407 ettNextGen[990:187961] 21.2999994.26000010.0000002.000000

转载请注明原文地址: https://www.6miu.com/read-2000134.html

最新回复(0)