nsmutablearray float对象返回零

时间:2011-07-09 15:17:07

标签: iphone objective-c ios xcode

在根模型中我有:

[self.rPrices replaceObjectAtIndex:0
                        withObject:[NSNumber numberWithFloat:(float)nR4]]; 
NSLog(@"%.2f", [self.rPrices objectAtIndex:0]);

其中rPrices为NSMutableArray

nR4不为零,但上述NSLog(...);显示为零。

由于

2 个答案:

答案 0 :(得分:9)

试试这个

NSLog(@"%.2f", [[self.rPrices objectAtIndex:0] floatValue]);

或者只是将NSNumber打印为对象

NSLog(@"%@", [self.rPrices objectAtIndex:0]);

答案 1 :(得分:2)

NSNumber是一个对象。所以你不能使用浮点格式说明符“%f”打印它。

您可以使用“%@”或使用-floatValue从中获取浮动值,然后使用“%f”打印。

相关问题