Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSNumberFormatter only rounds to 3 decimal places

I'm trying to use NSNumberFormatter to round a number to 5 decimal places in an iPhone app, but [formatter stringFromNumber:] always returns strings rounded to 0.001 (3 decimal places). What am I doing wrong?

formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setRoundingMode:NSNumberFormatterRoundHalfUp];
[formatter setSecondaryGroupingSize:3];
[formatter setRoundingIncrement:[NSNumber numberWithDouble:0.00001]];
like image 621
ChadK Avatar asked May 01 '09 04:05

ChadK


2 Answers

Try -setMaximumFractionDigits: and -setMinimumFractionDigits:

These configure the number of digits after the decimal separator.

like image 103
Nathan Kinsinger Avatar answered Nov 17 '22 11:11

Nathan Kinsinger


Try [formatter setFormat:@"0.00000"]; instead of setRoundingIncrement:.

like image 29
smorgan Avatar answered Nov 17 '22 11:11

smorgan