Adding Thousand Separator to Int in Swift

后端 未结 7 1290
长发绾君心
长发绾君心 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:53

    You want to use NSNumberFormatter:

    let fmt = NSNumberFormatter()
    fmt.numberStyle = .DecimalStyle
    fmt.stringFromNumber(2358000)  // with my locale, "2,358,000"
    fmt.locale = NSLocale(localeIdentifier: "fr_FR")
    fmt.stringFromNumber(2358000)  // "2 358 000"
    

提交回复
热议问题