let value:Float = 5.678434 let roundedValue = round(value * 100) / 100
roundedValue Float 5.6784339
如何获得5.67或5.68?
答案 0 :(得分:0)
extension Float {
func rounded(toPlaces places:Int) -> Float {
let divisor = pow(10.0, Float(places))
return (self * divisor).rounded() / divisor
}
}
let floatNumber:Float = 3.67565676
print(floatNumber.rounded(toPlaces: 2))
//打印3.68