简单的方法:
result = (isTitle)? @"Leg Span" : [NSString stringWithFormat:@"%dcm / %.2fin", (int)legCentimetres, legInches];
但是对于那些期望小数点分隔符以外的东西的人来说,这并不能正确地格式化数字。为此,您应该使用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];