当我将NSString转换为float时出现问题

时间:2015-10-25 19:00:28

标签: ios floating-point nsstring type-conversion

将NSString转换为float时出现问题。我没有价值,但为什么呢?

NSString *polylineStr = @"51.351305,-0.024239;51.374855,-0.092412";
NSArray *strPolylineArr = [polylineStr componentsSeparatedByString:@";"];
for ( NSString *str in strPolylineArr) {

    NSArray *arr = [str componentsSeparatedByString:@","];
    NSString *firstString = [arr firstObject];
    NSLog(@"__str %@ float %f", [arr firstObject], [firstString floatValue]);
}

我的输出是

__str 51.351305 float 0.000000
__str 51.374855 float 51.374855

我发现第一个字符串出错了。

enter image description here

1 个答案:

答案 0 :(得分:0)

你的字符串中的HI有些字符添加:''你看不到复制字符在单引号上或使用下面的代码:

NSString *polylineStr = [NSString stringWithFormat:@"51.351305,-0.024239;51.374855,-0.092412"];
    NSArray *strPolylineArr = [polylineStr componentsSeparatedByString:@";"];
    for ( NSString *str in strPolylineArr) {

        NSArray *arr = [str componentsSeparatedByString:@","];
        NSString *firstString = [arr firstObject];
        NSLog(@"__str %@ float %f", [arr firstObject], [firstString floatValue]);
    }