Reputation: 4562
Does anyone know the way to format a double value to a String without thousand separator (In Swift - ios).
Note: I need to consider the current locale as some european languages use comma (,) as decimal separator. for example german languange write 12.50 as 12,50.
So if the double value is 1250.25
English-uk : 1250.25
German : 1250,25
if I use following two ways (Whn english UK) I am getting the value as (1,212.50). BUT I want to format the value to String without thousant separator. So in this case 1212.50
var testOne = String.localizedStringWithFormat("%.2f", 1212.50)
var testTwo = String(format: "%.2f", locale: NSLocale.currentLocale(), 1212.50)
Upvotes: 0
Views: 3822
Reputation: 1209
You need to use the NSNumberFormatter on the number and explicitly set the grouping separator to empty string:
let formatter = NSNumberFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US") // locale determines the decimal point (. or ,); English locale has "."
formatter.groupingSeparator = "" // you will never get thousands separator as output
formatter.minimumFractionDigits = 2
formatter.maximumFractionDigits = 2
formatter.numberStyle = .DecimalStyle
let result = formatter.stringFromNumber(1233.99) // you will always get string 1233.99
Upvotes: 6