Search code examples
iosswiftnsnumberformatter

How/where do I use NSNumberFormatter?


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?


Solution

  • 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