Hav*_*Lin 4 iphone xcode nsstring ios
我用这段代码来剪切字符串
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]);
Run Code Online (Sandbox Code Playgroud)
但为什么它会回来
22.225454330444336和113.554008483886719
由于浮点数不准确,因此可以通过调用doubleValue而不是floatValue:
NSLog(@"second %.15f", [[array objectAtIndex:1] doubleValue]);
Run Code Online (Sandbox Code Playgroud)
这不是问题componentsSeparatedByString:.