2013-06-06 3 views
0

'legInches'를 반올림했으나 소수점 대신 2 자리로 반올림하려면 어떻게해야합니까?소수 자릿수 2 자릿수로 반올림

구체적으로 - (INT) 라운드 (legInches)

result = (isTitle)? @"Leg Span" : [NSString stringWithFormat:@"%dcm/%din", (int)legCentimetres, (int)round(legInches)]; 

답변

1

간단한 방법 :

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]; 
+0

완벽! 감사! – user2461049

관련 문제