美文网首页
NSArray 快速求和、平均值、最大值、最小值

NSArray 快速求和、平均值、最大值、最小值

作者: 奉灬孝 | 来源:发表于2018-10-10 16:34 被阅读5次

    NSArray *arr = @[@"5.0",@"1.0",@"4.0",@"3.0",@"4.4",@"10.7",@"6.0",@"14.5",@"16.4",@"30.2",@"20.8",@"18.6"];
    float sum = [[arr valueForKeyPath:@"@sum.floatValue"] floatValue];//求和
    float avg = [[arr valueForKeyPath:@"@avg.floatValue"] floatValue];//求平均值
    float max = [[arr valueForKeyPath:@"@max.floatValue"] floatValue];//求最大值
    float min = [[arr valueForKeyPath:@"@min.floatValue"] floatValue];//求最小值
    NSLog(@"和:%f \n 平均值:%f \n 最大值:%f \n 最小值:%f",sum,avg,max,min);

    相关文章

      网友评论

          本文标题:NSArray 快速求和、平均值、最大值、最小值

          本文链接:https://www.haomeiwen.com/subject/vgvdaftx.html