My problem is that I'm currently using a NSNumberFormatter
with NSNumberFormatterDecimalStyle
. I'm using this because I want to have the commas separate the numbers every three numbers just like one would write it when I turn it into a string. This works great however the precision of the decimal place is quite small. It's only precise to the thousandths place. For my application I need it to be accurate to the 12th decimal place. I can do it very easily with the %g format specifier by just putting %.12g in the string format but I don't know how I can achieve this with the NSNumberFormatter. I tried removing the format: NSNumberFormatterDecimalStyle
but that just makes my number an integer. Any help would be appreciated.
Here is the code I'm using currently to format the double: number
.
NSNumber *theNumber = [NSNumber numberWithDouble:number];
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[num setNumberStyle:NSNumberFormatterDecimalStyle];
[num setGroupingSeparator:@","];
Just set the minimum or digits to the right of the decimal point:
- (void)setMinimumFractionDigits:(NSUInteger)number
Example:
NSNumber *number = @123456.654321;
NSNumberFormatter *formatter = [NSNumberFormatter new];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setGroupingSeparator:@","];
[formatter setMinimumFractionDigits:12];
NSString *stringNumber = [formatter stringFromNumber:number];
NSLog(@"Number: '%@'", stringNumber);
NSLog output
Number: '123,456.654321000000'