其他iOS开发进阶基础知识拾遗

iOS中的数值处理问题

2016-01-07  本文已影响6105人  YM_1

必知的三个C函数

ceil(x)返回不小于x的最小整数值(然后转换为double型)。
floor(x)返回不大于x的最大整数值。
round(x)返回x的四舍五入整数值。

上面就是天花板函数、地板函数、四舍五入函数。

保留两位小数,四舍五入

    //保留两位小数,四舍五入
    CGFloat rounded_up = round(0.355 * 100) / 100;
    NSLog(@"%.2lf",rounded_up);
    
    
    //保留两位小数,直接进1(天花板函数)
    CGFloat rounded_up1 = ceilf(0.355 * 100) / 100;
    NSLog(@"%.2lf",rounded_up1);
    
    //保留两位小数,舍弃后面所有位数。(地板函数)
    CGFloat rounded_up2 = floor(0.355 * 100) / 100;
    NSLog(@"%.2lf",rounded_up2);
63C695AC-1EF9-4915-9BA2-2026B08437A3.png
    //保留两位小数,四舍五入
    CGFloat rounded_up = round(0.354 * 100) / 100;
    NSLog(@"%.2lf",rounded_up);
    
    //保留两位小数,直接进1(天花板函数)
    CGFloat rounded_up1 = ceilf(0.354 * 100) / 100;
    NSLog(@"%.2lf",rounded_up1);
    
    //保留两位小数,舍弃后面所有位数。(地板函数)
    CGFloat rounded_up2 = floor(0.354 * 100) / 100;
    NSLog(@"%.2lf",rounded_up2);

OU0KG9OCH$OWGM8W16G1`{G.jpg
上一篇 下一篇

猜你喜欢

热点阅读