How to truncate decimals to x places in Swift

前端 未结 9 1941
Happy的楠姐
Happy的楠姐 2020-12-04 01:26

In my swift program, I have a really long decimal number (say 17.9384693864596069567) and I want to truncate the decimal to a few decimal places (so I want the

9条回答
  •  Happy的楠姐
    2020-12-04 01:59

    You can keep it simple with:

    String(format: "%.0f", ratio*100)
    

    Where 0 is the amount of decimals you want to allow. In this case zero. Ratio is a double like: 0.5556633. Hope it helps.

提交回复
热议问题