我从JSON中获取纬度和经度。这些值以字符串的形式出现。
我将该值存储在CLLocationDegrees中,如下所示:
CLLocationDegrees lat = [[coordinates objectForKey:@"lat"] doubleValue];
CLLocationDegrees lon = [[coordinates objectForKey:@"lat"] doubleValue];但是,当我稍后从lat和lon检索值并将它们放入NSString时,该值与JSON提供的值不同。更改通常只是坐标的最后几位数,但这足以引起问题。
NSString *latitude = [[NSNumber numberWithDouble:lat] stringValue];
NSString *longitude = [[NSNumber numberWithDouble:lon] stringValue];基本上,在这一点上,[coordinates objectForKey:@"lat"]不等于latitude。
任何关于如何解决这一问题的建议都将不胜感激。
发布于 2016-05-04 23:04:19
NSNumber stringValue将给出一个默认的表示,通常只有5位小数。
一种选择是使用stringWithFormat并使用与原始JSON中的数据相匹配的格式。
NSString *latitude = [NSString stringWithFormat:@"%.7f", lat];调整格式说明符,以满足您的确切需要。
另外,请记住,从原始字符串到double可能会导致一些精度的损失,这是由于浮点数的表示方式所致。除了保持对原始字符串的引用(如果它是那么重要的话),没有什么可以做的。
https://stackoverflow.com/questions/37038731
复制相似问题