Reputation: 3
I have rounded the 'legInches' but how do I round this to 2 decimal points instead of none?
specifically - (int)round(legInches)
result = (isTitle)? @"Leg Span" : [NSString stringWithFormat:@"%dcm / %din", (int)legCentimetres, (int)round(legInches)];
Upvotes: 0
Views: 84
Reputation: 318804
The simple approach:
result = (isTitle)? @"Leg Span" : [NSString stringWithFormat:@"%dcm / %.2fin", (int)legCentimetres, legInches];
But this doesn't format the number properly for people that expect something other than a period for the decimal separator. For that you should use an NSNumberFormatter
.
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setMaximumFractionDigits:2];
NSString *legInchesStr = [formatter stringFromNumber:@(legInches)];
result = (isTitle)? @"Leg Span" : [NSString stringWithFormat:@"%dcm / %@in", (int)legCentimetres, legInchesStr];
Upvotes: 1