NSString *pi = @"3.14159265";
int floatSize = 3;
NSDecimalNumber *numericValue = [NSDecimalNumber decimalNumberWithString:pi];
NSDecimalNumberHandler *roundingStyle = [NSDecimalNumberHandler
decimalNumberHandlerWithRoundingMode:
NSRoundBankers scale:floatSize raiseOnExactness:NO raiseOnOverflow:NO
raiseOnUnderflow:NO
raiseOnDivideByZero:NO];
NSDecimalNumber *roundedNumber = [numericValue
decimalNumberByRoundingAccordingToBehavior:
roundingStyle];
or
NSString *pi = @"3.14159265";
NSString *roundedNumber = [[NSString alloc] initWithFormat:
@"%.3f",[pi floatValue]];
Result: 3.142
1 comment:
Thank you so much! This is the only place I could find real rounding without formatters, and other non-sense.
Post a Comment