How/where do I use NSNumberFormatter?

折月煮酒 提交于 2019-12-20 07:48:05

问题


I'm new to code and after reviewing a few answers still need a hand with this.

In my code:

func labelInformation(){
     numLabels.text = newLabel.text
} 

Current result:

228500.23

Desired result:

228,500.23

How/where do I use NSNumberFormatter?


回答1:


Try like this:

let inputValue = 228500.23

let numberFormatter = NSNumberFormatter()

numberFormatter.numberStyle = .CurrencyStyle
numberFormatter.currencySymbol = ""

let outputString = numberFormatter.stringFromNumber(inputValue) ?? "0.00"

print(outputString)   // 228,500.23


来源:https://stackoverflow.com/questions/34360857/how-where-do-i-use-nsnumberformatter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!