IOS 四舍五入 进一法 去尾法

float numberToRound;

   int result;

 

    numberToRound = 4.51;

    result = (int)roundf(numberToRound);

    NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(4.510000) = 5


    result = (int)ceilf(numberToRound);

    NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(4.510000) = 5


    result = (int)floorf(numberToRound);

    NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(4.510000) = 4


    numberToRound = 10.49;

    result = (int)roundf(numberToRound);

    NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(10.490000) = 10


    result = (int)ceilf(numberToRound);

    NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(10.490000) = 11


    result = (int)floorf(numberToRound);

    NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(10.490000) = 10


    numberToRound = -2.49;

    result = (int)roundf(numberToRound);

    NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(-2.490000) = -2


    result = (int)ceilf(numberToRound);

    NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(-2.490000) = -2


    result = (int)floorf(numberToRound);

    NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(-2.490000) = -3


    numberToRound = -3.51;

    result = (int)roundf(numberToRound);

    NSLog(@"roundf(%f) = %d", numberToRound, result); // roundf(-3.510000) = -4


    result = (int)ceilf(numberToRound);

    NSLog(@"ceilf(%f) = %d", numberToRound, result); // ceilf(-3.510000) = -3


    result = (int)floorf(numberToRound);

    NSLog(@"floorf(%f) = %d", numberToRound, result); // floorf(-3.510000) = -4

posted on   雨季的雾  阅读(859)  评论(0编辑  收藏  举报
编辑推荐:
· 如何做好软件架构师
· 记录一次线上服务OOM排查
· Linux实时系统Xenomai宕机问题的深度定位过程
· 记一次 .NET某汗液测试机系统 崩溃分析
· 深度解析Mamba与状态空间模型:一图带你轻松入门
阅读排行:
· 如何做好软件架构师
· 记录一次线上服务OOM排查
· Bogus:.NET的假数据生成利器
· [.NET] 单位转换实践:深入解析 Units.NET
· itextpdf 找出PDF中 文字的坐标
< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示