Birju Birju - 1 year ago 74
Objective-C Question

How to add dot(.) after every 3 digit?

I have number like 2000000 and i want this number in 2.000.000 while displaying.I tried to use number formatter which is provided by iOS sdk

but it adds comma(,).

Thanks in advance.

Answer Source

You can use the groupingSeparator property of the NSNumberFormatter for that.


NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.usesGroupingSeparator = YES;
formatter.groupingSeparator = @".";
NSNumber *number = @(2000000);
NSString *string = [formatter stringFromNumber:number];

Swift 2.2

let formatter = NSNumberFormatter()
formatter.usesGroupingSeparator = true
formatter.groupingSeparator = "."
let number = 2000000
let string = formatter.stringFromNumber(number)