2012-03-15 135 views
4

我用这个代码把绳剪断componentsSeparatedByString返回错误的结果

NSString *titleString = @"22.225453615805794,113.554006577014889"; 
    NSArray *array = [titleString componentsSeparatedByString:@","]; 
    NSLog(@"title string %@", titleString); 
    NSLog(@"first %.15f", [[array objectAtIndex:0] floatValue]); 
    NSLog(@"second %.15f", [[array objectAtIndex:1] floatValue]); 

但为什么它返回

22.225454330444336 和 113.554008483886719

回答

7

由于浮点数字是不准确的,你可以通过调用doubleValue,而不是floatValue得到更高的精度:

NSLog(@"second %.15f", [[array objectAtIndex:1] doubleValue]); 

这不是componentsSeparatedByString:问题。

+0

是的,你是对的,非常感谢 – 2012-03-15 08:38:17

1

我认为这是在将字符串转换成浮动的问题。尝试使用双。