これは数学ではなく文字列操作であるため、結果の値は丸められません。
NSRange range = [price rangeOfString:@"."];
if (range.location != NSNotFound) {
NSInteger index = MIN(range.location+2, price.length-1);
NSString *truncated = [price substringToIndex:index];
}
これは主に文字列操作であり、NSString をだましてその計算を実行させます。
NSString *roundedPrice = [NSString stringWithFormat:@"%.02f", [price floatValue]];
または、すべての数値を数値として保持し、文字列をユーザーに提示する単なる方法と考えることもできます。そのためには、NSNumberFormatter を使用します。
NSNumber *priceObject = // keep these sorts values as objects
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
NSString *presentMeToUser = [numberFormatter stringFromNumber:priceObject];
// you could also keep price as a float, "boxing" it at the end with:
// [NSNumber numberWithFloat:price];