iOS float转换为int并就近取整的问题

本文介绍了如何使用 C 语言中的 round(), roundf(), lround(), 和 lroundf() 函数将浮点数转换为整数,并进行了四舍五入处理。还提供了一个实例,展示如何在计算中应用这些函数。



        float duration = 0.029999999;

        if (duration == 0) {

            duration = 0.03; //(gif delay time default is 0.1)

        }

        NSLog(@"the delay is %f", duration);

        NSLog(@"the duration * 100 is %f", duration * 100);

       NSUInteger interValue = roundf(duration * 100);


        NSLog(@"the roundf is %lu", (unsigned long)interValue);

        

        size_t delayTime = (int)( (duration * 1000) / 10 );


将一个float转换为int并就近取整的问题 函数round(), roundf(), lround(), 和lroundf() 


float fractionalPage = _scrollVIew.contentOffset.x / pageWidth ;
    NSInteger nearestNumber = lround(fractionalPage) ;

extern float ceilf(float);

extern double ceil(double);

extern long double ceill(long double);


extern float floorf(float);

extern double floor(double);

extern long double floorl(longdouble);


extern float roundf(float);

extern double round(double);

extern long double roundl(longdouble);


round:如果参数是小数,则求本身的四舍五入。
ceil:如果参数是小数,则求最小的整数但不小于本身.
floor:如果参数是小数,则求最大的整数但不大于本身. 

Example:如何值是3.4的话,则

3.4 -- round 3.000000

    -- ceil 4.000000

    -- floor 3.00000


CGRectMake(floorf(self.view.bounds.size.width*0.5f -39.f*0.5f),self.view.bounds.size.height -573939)

其中floorf(self.view.bounds.size.width*0.5f -39.f*0.5f)返回值为

140.000000这种形式


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值