Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift how to format a large number with thousands separators?

Tags:

Is there a simple command to format 1.60543e+06 to 1,605,436???

resultFV.text = String.localizedStringWithFormat("%f", fv)

does not get it.

like image 394
Mike D Avatar asked Mar 09 '15 06:03

Mike D


People also ask

What is the thousand separator format?

The character used as the thousands separatorIn the United States, this character is a comma (,). In Germany, it is a period (.). Thus one thousand and twenty-five is displayed as 1,025 in the United States and 1.025 in Germany. In Sweden, the thousands separator is a space.


2 Answers

In Swift 3,

NumberFormatter.localizedString(from: NSNumber(value: whatever), number: NumberFormatter.Style.decimal) 
like image 75
JLundell Avatar answered Oct 23 '22 11:10

JLundell


Swift Xcode 6.3, SOLVED (I decided to leave the $ in the code). If you don't want a $ in the output, change .CurrencyStyle to .DecimalStyle

var fv = 3534234.55  var formatter = NSNumberFormatter() formatter.numberStyle = .CurrencyStyle formatter.maximumFractionDigits = 0; resultFV.text = formatter.stringFromNumber(fv) // result: $3,534,235 – 
like image 35
Mike D Avatar answered Oct 23 '22 11:10

Mike D