Swift - Remove Trailing Zeros From Double

前端 未结 4 1332
栀梦
栀梦 2021-01-03 19:07

What is the function that removes trailing zeros from doubles?

var double = 3.0
var double2 = 3.10

println(func(double)) // 3
println(func(double2)) // 3.1
         


        
4条回答
  •  时光取名叫无心
    2021-01-03 19:39

    You can do it this way but it will return a string:

    var double = 3.0
    var double2 = 3.10
    
    func forTrailingZero(temp: Double) -> String {
        var tempVar = String(format: "%g", temp)
        return tempVar
    }
    
    forTrailingZero(double)   //3
    forTrailingZero(double2)  //3.1
    

提交回复
热议问题