我尝试比较浮点值,但我做不到,我使用以下代码:
float a = [[array objectAtIndex:(i+1)] floatValue];
float b = [[array objectAtIndex:(i)] floatValue];
a = [[NSString stringWithFormat:@"%.2f",a] floatValue];
b = [[NSString stringWithFormat:@"%.2f",b] floatValue];
step = [[NSString stringWithFormat:@"%.2f",step] floatValue];
float newStep = a-b;
if (newStep != step) {
NSLog(@"NewStep: %f Step: %f",newStep,step);
}
这是输出:
2013-04-28 19:07:57.396 Pro[24265:c07] NewStep: 0.220000 Step: 0.220000
2013-04-28 19:07:57.397 Pro[24265:c07] NewStep: 0.220000 Step: 0.220000
2013-04-28 19:07:57.397 Pro[24265:c07] NewStep: 0.220000 Step: 0.220000
2013-04-28 19:07:57.397 Pro[24265:c07] NewStep: 2.420000 Step: 0.220000
2013-04-28 19:07:57.397 Pro[24265:c07] NewStep: 0.220000 Step: 0.220000
2013-04-28 19:07:57.397 Pro[24265:c07] NewStep: 0.220000 Step: 0.220000
显然不起作用,错误在哪里?代码似乎是正确的!