我解析一個字符串,它看起來像這樣:解析字符串,得到一個數字並獲得不同的結果
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的方式,然後使用雙?
雙重不準確。如果您真的關心精確浮點值,請使用小數。編輯:對不起,我認爲這是關於C#:)也許這同樣適用於這裏。 – Silvermind 2012-03-26 15:44:57
我沒有看到你得到不同的結果。在第一種情況下,您只是獲得默認的6.4格式化精度。更改格式化精度,數字會更相似(如果不相同)。 – 2012-03-26 15:53:26