@H_404_1@
我想将我的值舍入到最接近的5美分,例如:
5.31 -> 5.30 5.35 -> 5.35 5.33 -> 5.35 5.38 -> 5.40
目前我是通过使用以下方式获取小数值来实现的:
let numbers = 5.33 let decimal = (numbers - rint(numbers)) * 100 let rounded = rint(numbers) + (5 * round(decimal / 5)) / 100 // This results in 5.35
我想知道是否有一个更好的方法,步骤更少,因为有时数字 – rint(数字)给我一个奇怪的结果,如:
let numbers = 12.12 let decimal = (numbers - rint(numbers)) * 100 // This results in 11.9999999999999
解决方法
事实证明……真的很简单
let x: Float = 1.03 //or whatever value,you can also use the Double type let y = round(x * 20) / 20
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。