我想将我的值舍入到最接近的5美分,例如: 5.31 - 5.305.35 - 5.355.33 - 5.355.38 - 5.40 目前我是通过使用以下方式获取小数值来实现的: let numbers = 5.33let decimal = (numbers - rint(numbers)) * 100let roun
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