iOS 向下取整、向上取整、四舍五入
生活随笔
收集整理的這篇文章主要介紹了
iOS 向下取整、向上取整、四舍五入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
向上取整:float ceilf(float); double ceil(double);
向下取整:float floorf(float); double floor(double);
四舍五入:float roundf(float); double round(double);
CGFloat a = 3.3;
CGFloat ceilA = ceilf(a);
CGFloat floorA = floorf(a);
CGFloat roundA = roundf(a);
NSLog(@"%lf 向上取整為%lf, 向下取整為%lf, 四舍五入為%lf", a, ceilA, floorA, roundA);
double b = 5.8;
double ceilB = ceil(b);
double floorB = floor(b);
double roundB = round(b);
NSLog(@"%lf 向上取整為%lf, 向下取整為%lf, 四舍五入為%lf", b, ceilB, floorB, roundB);
CGFloat c = -3.3;
CGFloat ceilC = ceilf(c);
CGFloat floorC = floorf(c);
CGFloat roundC = roundf(c);
NSLog(@"%lf 向上取整為%lf, 向下取整為%lf, 四舍五入為%lf", c, ceilC, floorC, roundC);
double d = -5.8;
double ceilD = ceil(d);
double floorD = floor(d);
double roundD = round(d);
NSLog(@"%lf 向上取整為%lf, 向下取整為%lf, 四舍五入為%lf", d, ceilD, floorD, roundD);
// 打印結果
2017-03-02 10:03:47.570 UsingWebView[88462:10650303] 3.300000 向上取整為4.000000, 向下取整為3.000000, 四舍五入為3.000000
2017-03-02 10:03:47.572 UsingWebView[88462:10650303] 5.800000 向上取整為6.000000, 向下取整為5.000000, 四舍五入為6.000000
2017-03-02 10:03:47.573 UsingWebView[88462:10650303] -3.300000 向上取整為-3.000000, 向下取整為-4.000000, 四舍五入為-3.000000
2017-03-02 10:03:47.573 UsingWebView[88462:10650303] -5.800000 向上取整為-5.000000, 向下取整為-6.000000, 四舍五入為-6.000000
總結
以上是生活随笔為你收集整理的iOS 向下取整、向上取整、四舍五入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php命令执行无法重定向输出,php
- 下一篇: 转义字符