Bins Ich
Bins Ich

Reputation: 1842

iOS Round Double value to 2 decimal digits and round 3rd decimal digit up

I have I double value like this one 17.125. It should be rounded up to 17.13

If I use the simple method like %.2f it shows me 17.12 I also followed several methods described in other threads here like using NumberFormatter and so on - but without luck.

Maybe someone has an advice for me to fix this problem? Do I have to round it on my own?

Upvotes: 2

Views: 2565

Answers (1)

Dipendra
Dipendra

Reputation: 620

Try This:

float number=17.125;
NSNumberFormatter *format = [[NSNumberFormatter alloc]init];
[format setNumberStyle:NSNumberFormatterDecimalStyle];
[format setRoundingMode:NSNumberFormatterRoundHalfUp];
[format setMaximumFractionDigits:2];
NSString *temp = [format stringFromNumber:[NSNumber numberWithFloat:number]];
NSLog(@"%@",temp);

Upvotes: 10

Related Questions