IOS 四舍五入 进一法 去尾法
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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何做好软件架构师
· 记录一次线上服务OOM排查
· Linux实时系统Xenomai宕机问题的深度定位过程
· 记一次 .NET某汗液测试机系统 崩溃分析
· 深度解析Mamba与状态空间模型:一图带你轻松入门
· 如何做好软件架构师
· 记录一次线上服务OOM排查
· Bogus:.NET的假数据生成利器
· [.NET] 单位转换实践:深入解析 Units.NET
· itextpdf 找出PDF中 文字的坐标