2012-03-26 55 views
0

我解析一個字符串,它看起來像這樣:解析字符串,得到一個數字並獲得不同的結果

POINT(-96.795894101604 46.911266990766) 

當我解析2號的時候,我如果我用得到基於不同的價值觀雙或[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 

爲什麼我得到一個更精確的值WH恩NSString/NSNumber的方式,然後使用雙?

+0

雙重不準確。如果您真的關心精確浮點值,請使用小數。編輯:對不起,我認爲這是關於C#:)也許這同樣適用於這裏。 – Silvermind 2012-03-26 15:44:57

+0

我沒有看到你得到不同的結果。在第一種情況下,您只是獲得默認的6.4格式化精度。更改格式化精度,數字會更相似(如果不相同)。 – 2012-03-26 15:53:26

回答

1

嘗試。

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

字符串表示的區別並不意味着值的差異。

2

首先,你可以簡化你的代碼:

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); 

注意,事實上,你得到的tempLattempLonlatitudelongitude的值,這樣就不能比他們更精確。

唯一的問題是您正在使用格式%g打印latitudelongitude的值。嘗試使用其他格式的精度,你會看到更多的小數位:

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

我知道代碼可以簡化,我只是測試,所以不太在意它。當我使用%f時,我獲得了相同的精確度,嘗試使用%g並將其保留。你是對的,它只是一個格式化的「錯誤」,其中對象將打印更多的數字,然後加倍。正如Michex指出的,直接比較它們是平等的。 – Padin215 2012-03-26 16:11:05

+0

我簡化了代碼,以顯示您從'latitude'中獲得'tempLat'的值。這意味着「緯度」不能有*「小」*精度。 「longitude」和「tempLong」同樣適用。 – sch 2012-03-26 16:13:36

+0

啊,好吧,我必須在那部分上面看到,謝謝。 – Padin215 2012-03-26 18:59:00