Format string with trailing zeros removed for x decimal places in Swift [duplicate]

白昼怎懂夜的黑 提交于 2019-12-03 12:38:01
Icaro

You have to use NumberFormatter:

    let formatter = NumberFormatter()
    formatter.minimumFractionDigits = 0
    formatter.maximumFractionDigits = 2
    print(formatter.string(from: 1.0000)!) // 1
    print(formatter.string(from: 1.2345)!) // 1.23

This example will print 1 for the first case and 1.23 for the second; the number of minimum and maximum decimal places can be adjusted as you need.

I Hope that helps you!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!