Objective c 解析字符串以获取数字并获得不同的结果

Objective c 解析字符串以获取数字并获得不同的结果,objective-c,ios,Objective C,Ios,我正在分析一个如下所示的字符串: POINT(-96.795894101604 46.911266990766) 当我将这两个数字解析出来时,我会根据使用double或[NSNumber numberWithDouble:]得到不同的值 -(void)parseGPSValue:(NSMutableString *)GPSString { NSRange prefixMatch = [GPSString rangeOfString:@"("]; NSRange midMatch

我正在分析一个如下所示的字符串:

POINT(-96.795894101604 46.911266990766)
当我将这两个数字解析出来时,我会根据使用double或[NSNumber numberWithDouble:]得到不同的值

-(void)parseGPSValue:(NSMutableString *)GPSString
{
    NSRange prefixMatch = [GPSString rangeOfString:@"("];
    NSRange midMatch = [GPSString rangeOfString:@" "];
    NSRange sufixMatch = [GPSString rangeOfString:@")"];

    //parses the GPS coords; NSMakeRange takes starting location and range
    double latitude = [[GPSString substringWithRange:NSMakeRange((prefixMatch.location + 1), ((midMatch.location - 0) - (prefixMatch.location + 1)))] doubleValue];
    double longitude = [[GPSString substringWithRange:NSMakeRange(midMatch.location + 1, (sufixMatch.location) - (midMatch.location + 1))] doubleValue];

    NSString *temp1 = [GPSString substringWithRange:NSMakeRange((prefixMatch.location + 1), ((midMatch.location - 0) - (prefixMatch.location + 1)))];
    NSString *temp2 = [GPSString substringWithRange:NSMakeRange(midMatch.location + 1, (sufixMatch.location) - (midMatch.location + 1))];

    NSNumber *tempLat = [NSNumber numberWithDouble:[temp1 doubleValue]];
    NSNumber *tempLong = [NSNumber numberWithDouble:[temp2 doubleValue]];

    NSLog(@"GPSString: %@", GPSString);

    NSLog(@"latitude: %g| longitude: %g|", latitude, longitude);
    NSLog(@"NSNumber lat: %@, NSNumberLong: %@", tempLat, tempLong);
}
结果如下:

2012-03-26 10:33:10.166 GPSString: POINT(-96.795894101604 46.911266990766)
2012-03-26 10:33:10.167 latitude: -96.7959| longitude: 46.9113|
2012-03-26 10:33:10.168 NSNumber lat: -96.79589410160401, NSNumberLong: 46.911266990766

为什么在使用NSString/NSNumber时,我会得到更精确的值,然后使用double?

首先,您可以将代码简化为:

double latitude = [temp1 doubleValue];
double longitude = [temp2 doubleValue];

NSNumber *tempLat = [NSNumber numberWithDouble:latitude]; // Can't be more precise than latitude 
NSNumber *tempLong = [NSNumber numberWithDouble:longitude]; // Can't be more precise than longitude 

NSLog(@"latitude: %g| longitude: %g|", latitude, longitude);
NSLog(@"NSNumber lat: %@, NSNumberLong: %@", tempLat, tempLong);
请注意,事实上,您可以从
纬度
经度
获得
tempLat
templan
的值,因此它们的精度不可能比它们高

唯一的问题是,您正在使用
%g
格式打印
纬度
经度
的值。尝试其他格式精度,您将看到更多小数:

NSLog(@"latitude: %.12f| longitude: %.12f|", latitude, longitude);
// latitude: -96.795894101604| longitude: 46.911266990766|

首先,您可以将代码简化为:

double latitude = [temp1 doubleValue];
double longitude = [temp2 doubleValue];

NSNumber *tempLat = [NSNumber numberWithDouble:latitude]; // Can't be more precise than latitude 
NSNumber *tempLong = [NSNumber numberWithDouble:longitude]; // Can't be more precise than longitude 

NSLog(@"latitude: %g| longitude: %g|", latitude, longitude);
NSLog(@"NSNumber lat: %@, NSNumberLong: %@", tempLat, tempLong);
请注意,事实上,您可以从
纬度
经度
获得
tempLat
templan
的值,因此它们的精度不可能比它们高

唯一的问题是,您正在使用
%g
格式打印
纬度
经度
的值。尝试其他格式精度,您将看到更多小数:

NSLog(@"latitude: %.12f| longitude: %.12f|", latitude, longitude);
// latitude: -96.795894101604| longitude: 46.911266990766|
试试这个

if (latitude == [tempLat doubleValue]) {
    NSLog(@"same");
}
if (longitude == [tempLong doubleValue]) {
    NSLog(@"same");
}
字符串表示的不同并不意味着值的不同。

试试这个

if (latitude == [tempLat doubleValue]) {
    NSLog(@"same");
}
if (longitude == [tempLong doubleValue]) {
    NSLog(@"same");
}

字符串表示的不同并不意味着值的不同。

double是不精确的。如果您真正关心精确的浮点值,请使用十进制。编辑:对不起,我以为这是关于c:)也许同样适用于这里。我看不出你得到了不同的结果。在第一种情况下,您只需要获得默认的6.4格式精度。更改格式精度,数字将更加相似(如果不相同)。双精度是不精确的。如果您真正关心精确的浮点值,请使用十进制。编辑:对不起,我以为这是关于c:)也许同样适用于这里。我看不出你得到了不同的结果。在第一种情况下,您只需要获得默认的6.4格式精度。更改格式精度,数字将更加相似(如果不相同的话)。我知道代码可以简化,我只是在测试,所以不太关心它。当我使用%f时,我得到了相同的精度,尝试使用%g,但没有使用。您是对的,这只是一个格式化“错误”,对象将打印更多的数字,而不是双精度。正如Michex所指出的,当直接比较时,它们是相等的。我简化了代码,以显示您从
纬度
获得
模板的值。这意味着
纬度
的精度不能“更低”。这同样适用于
经度
tempLong
。我知道代码可以简化,我只是在测试,所以不太关心它。当我使用%f时,我得到了相同的精度,尝试使用%g,但没有使用。您是对的,这只是一个格式化“错误”,对象将打印更多的数字,而不是双精度。正如Michex所指出的,当直接比较时,它们是相等的。我简化了代码,以显示您从
纬度
获得
模板的值。这意味着
纬度
的精度不能“更低”。这同样适用于
经度
坦普隆