Adding Thousand Separator to Int in Swift

后端 未结 7 1268
长发绾君心
长发绾君心 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 05:58

    Code:

    //5000000
    let formatter = NumberFormatter()
    formatter.groupingSeparator = " "
    formatter.locale = Locale(identifier: "en_US")
    formatter.numberStyle = .decimal. 
    

    Output:

    5 000 000

提交回复
热议问题