Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

objective c - 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]];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try -setMaximumFractionDigits: and -setMinimumFractionDigits:

These configure the number of digits after the decimal separator.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...