iOS开发中经常遇到 浮点数的 四舍五入、进位、摸位 方法。
1,四舍五入法
floatnumberToRound;intresult; numberToRound =5.61; result = (int)roundf(numberToRound);NSLog(@"roundf(%.2f) = %d", numberToRound, result);//输出 roundf(5.61) = 6numberToRound =5.41; result = (int)roundf(numberToRound);NSLog(@"roundf(%.2f) = %d", numberToRound, result);//输出 roundf(5.41) = 5
2、进位方法
floatnumberToRound;intresult; numberToRound =5.61; result = (int)ceilf(numberToRound);NSLog(@"ceilf(%.2f) = %d", numberToRound, result);//输出 ceilf(5.61) = 6numberToRound =5.41; result = (int)ceilf(numberToRound);NSLog(@"ceilf(%.2f) = %d", numberToRound, result);//输出 ceilf(5.41) = 6
3、摸位方法
floatnumberToRound;intresult; numberToRound =5.61; result = (int)floorf(numberToRound);NSLog(@"floorf(%.2f) = %d", numberToRound, result);//输出 floorf(5.61) = 5numberToRound =5.41; result = (int)floorf(numberToRound);NSLog(@"floorf(%.2f) = %d", numberToRound, result);//输出 floorf(5.41) = 5
作者:龙翱天际
链接:https://www.jianshu.com/p/19d263a7410f
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
网友评论