Swift number formatting

前端 未结 4 1094
深忆病人
深忆病人 2021-02-08 08:10

I am just starting to get to know Swift but I am having a serious problem with number formatting at an extremely basic level.

For example, I need to display an integer w

4条回答
  •  时光说笑
    2021-02-08 08:24

    There is a simple solution I learned with "We <3 Swift", which is so easy you can even use without Foundation, round() or Strings, keeping the numeric value.

    Example:

    var number = 31.726354765
    var intNumber = Int(number * 1000.0)
    var roundedNumber = Double(intNumber) / 1000.0
    

    result: 31.726

提交回复
热议问题