不能在Float for Swift 4上工作

时间:2017-12-28 07:44:23

标签: swift rounding swift4

let value:Float = 5.678434
let roundedValue = round(value * 100) / 100

roundedValue Float 5.6784339

如何获得5.67或5.68?

1 个答案:

答案 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