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?

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);

http://stackoverflow.com/questions/15109963/ios-round-double-value-to-2-decimal-digits-and-round-3rd-decimal-digit-up

 
原文地址:https://www.cnblogs.com/savagemorgan/p/4071552.html