Swift - Remove Trailing Zeros From Double

前端 未结 4 1331
栀梦
栀梦 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:27

    In case you're looking how to remove trailing zeros from a string:

    string.replacingOccurrences(of: "0*$", with: "", options: .regularExpression)
    

    This will transform strings like "0.123000000" into "0.123"

提交回复
热议问题