I am trying to format a number, like this “123.123,00”. I am using an NSNumberFormatter.
The code is:
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setGroupingSize:3];
[numberFormatter setGroupingSeparator:@"."];
[numberFormatter setUsesGroupingSeparator:YES];
[numberFormatter setMaximumFractionDigits:12];
NSNumber *num = [NSNumber numberWithDouble:result];
NSString *formattedString = [numberFormatter stringFromNumber:num];
But it does not seem to work. It gives result like this “266.482107256796”. Any idea why the result is not “266.482,107,256,796”?
Grouping separator is used to separate digits in the integer part of a number and is not used in the fractional part. There’s no built-in way to achieve this effect. If you only support decimal numbers, you could manually parse the formatted string and insert a comma after each 3 characters, starting with the decimal separator.
Warning: this code is typed on an iPad (c).