谁能告诉我如何在Swift中舍入一个双数值到x位小数点后数位?
我有:
var totalWorkTimeInHours = (totalWorkTime/60/60)
totalWorkTime是一个NSTimeInterval (double),单位为秒。
totalWorkTimeInHours会给我小时数,但它给我的时间量是如此长的精确数字,例如1.543240952039......
当我打印totalWorkTimeInHours时,我如何将其四舍五入到1.543 ?
谁能告诉我如何在Swift中舍入一个双数值到x位小数点后数位?
我有:
var totalWorkTimeInHours = (totalWorkTime/60/60)
totalWorkTime是一个NSTimeInterval (double),单位为秒。
totalWorkTimeInHours会给我小时数,但它给我的时间量是如此长的精确数字,例如1.543240952039......
当我打印totalWorkTimeInHours时,我如何将其四舍五入到1.543 ?
当前回答
这个解决方案对我很有效。XCode 13.3.1 & Swift 5
extension Double {
func rounded(decimalPoint: Int) -> Double {
let power = pow(10, Double(decimalPoint))
return (self * power).rounded() / power
}
}
测试:
print(-87.7183123123.rounded(decimalPoint: 3))
print(-87.7188123123.rounded(decimalPoint: 3))
print(-87.7128123123.rounded(decimalPoint: 3))
结果:
-87.718
-87.719
-87.713
其他回答
对于许多应用程序,需要精确的小数位数来舍入。
对于其他一些,您没有这样的限制,并希望“压缩”输出大小,但仍然希望避免将数字转换为字符串(反之亦然),就像导出具有数百万个数字的JSON一样。
在这种情况下,你可以使用'trick'来四舍五入显数(尾数),而不是整数。在这种情况下,你会得到最后的小数点 大多数数字仍然最多保留3位小数点后(如果需要的话),而有些数字会稍微多一些。
这是你在使用十进制数时所期望的结果:
5.2472 5.2516 5.2556 5.26 5.264
而不是:
5.24731462499949 5.251488374999099 5.25566283399894 5.259839374999501 5.264012208999702
let value = 5.24731462499949
print(value)
// 5.24731462499949
let valueSignificandRounded = round((1000 * 10) * value.significand) / (1000 * 10)
let valueRounded = CGFloat(sign: v.sign, exponent: v.exponent, significand: valueSignificandRounded)
print(valueRounded)
// 5.2472
二重值舍入到小数的x位 不。小数点后的数
var x = 1.5657676754
var y = (x*10000).rounded()/10000
print(y) // 1.5658
var x = 1.5657676754
var y = (x*100).rounded()/100
print(y) // 1.57
var x = 1.5657676754
var y = (x*10).rounded()/10
print(y) // 1.6
Swift 2扩展
一个更通用的解决方案是以下扩展,适用于Swift 2和iOS 9:
extension Double {
/// Rounds the double to decimal places value
func roundToPlaces(places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(self * divisor) / divisor
}
}
Swift 3扩展
在Swift 3中,round被圆润取代:
extension Double {
/// Rounds the double to decimal places value
func rounded(toPlaces places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
}
返回Double四舍五入到小数点后4位的示例:
let x = Double(0.123456789).roundToPlaces(4) // x becomes 0.1235 under Swift 2
let x = Double(0.123456789).rounded(toPlaces: 4) // Swift 3 version
基于Yogi的回答,这里有一个Swift函数来完成这项工作:
func roundToPlaces(value:Double, places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(value * divisor) / divisor
}
:
Using String(format:): Typecast Double to String with %.3f format specifier and then back to Double Double(String(format: "%.3f", 10.123546789))! Or extend Double to handle N-Decimal places: extension Double { func rounded(toDecimalPlaces n: Int) -> Double { return Double(String(format: "%.\(n)f", self))! } } By calculation multiply with 10^3, round it and then divide by 10^3... (1000 * 10.123546789).rounded()/1000 Or extend Double to handle N-Decimal places: extension Double { func rounded(toDecimalPlaces n: Int) -> Double { let multiplier = pow(10, Double(n)) return (multiplier * self).rounded()/multiplier } }