美文网首页
iOS函数问题-floor、round、ceil

iOS函数问题-floor、round、ceil

作者: 守护地中海的花 | 来源:发表于2019-05-31 14:56 被阅读0次

floor-round-ceil

英文意思:floor:地板 round:变圆 ceil:天花板
因为项目里面 6p计算scrollView分页 和 滑动到顶部出现误差 所以这里需要重新审视一下 这些系统函数

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / self.contentBG.width;
    NSLog(@"scrollView.contentOffset.x:%f,self.contentBG.width:%f,%ld",scrollView.contentOffset.x,self.contentBG.width,index);
    CGFloat tmpOffsetX = scrollView.contentOffset.x / self.contentBG.width;
    NSLog(@"%f",tmpOffsetX);
    index = floor(tmpOffsetX);
    NSLog(@"index:%ld,floor(tmpOffsetX):%f",index,floor(tmpOffsetX));
    index = round(tmpOffsetX);
    NSLog(@"index:%ld,round(tmpOffsetX):%f",index,round(tmpOffsetX));
    index = ceil(tmpOffsetX);
    NSLog(@"index:%ld,ceil(tmpOffsetX):%f",index,ceil(tmpOffsetX));
    if (1 == 1.000000) {
        NSLog(@"6个0");
    }
    if (1 == 1.00) {
        NSLog(@"2个0");
    }
}

scrollView.contentOffset.x:376.333333,self.contentBG.width:376.464000,0
0.999653
index:0,floor(tmpOffsetX):0.000000
index:1,round(tmpOffsetX):1.000000
index:1,ceil(tmpOffsetX):1.000000
6个0
2个0

***以上出现误差 这时候分页索引应该用四舍五入round来取 *

参考文档iOS中的round、ceil、floor函数略解iOS开发之C语言函数库

  • floor
    向下取整 按小的来 负数也是
  • round
    四舍五入
  • ceil
    向上取整 按大的来 负数也是
  • fabs 绝对值

相关文章

网友评论

      本文标题:iOS函数问题-floor、round、ceil

      本文链接:https://www.haomeiwen.com/subject/ltnwtctx.html