首页 > 系统 > iOS > 正文

iOS 四舍五入 进一法 去尾法

2019-11-09 17:05:24
字体:
来源:转载
供稿:网友

//结果为0.468CGFloat tFloat = (CGFloat)375/800;//结果为0CGFloat tFloat = 375/800;//结果为0CGFloat tFloat = 1/0;//结果为+lnf 代表正无穷CGFloat tFloat = (CGFloat)1/0;

        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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表