Adding Thousand Separator to Int in Swift

后端 未结 7 1264
长发绾君心
长发绾君心 2020-11-30 05:17

I am fairly new to Swift and having a great deal of trouble finding a way to add a space as a thousand separator.

What I am hoping to achieve is taking the result

相关标签:
7条回答
  • 2020-11-30 06:11

    Try this

    func addPoints(inputNumber: NSMutableString){
        var count: Int = inputNumber.length
        while count >= 4 {
            count = count - 3
            inputNumber.insert(" ", at: count) // you also can use "," 
        }
        print(inputNumber)
    }
    

    The call:

    addPoints(inputNumber: "123456")
    

    The result:

    123 456 (or 123,456)

    0 讨论(0)
提交回复
热议问题