我使用NSLengthFormatter类来格式化用户和某个目标之间的距离。NSLengthFormatter get stringFromMeters:以英里/公里为单位
CLLocation *userLocation; //<- Coordinates fetched from CLLocationManager
CLLocation *targetLocation; //<- Some location retrieved from server data
CLLocationDistance distance = [userLocation distanceFromLocation:targetLocation];
NSLengthFormatter *lengthFormatter = [NSLengthFormatter new];
NSString *formattedLength = [lengthFormatter stringFromMeters:distance];
现在,如果长度小于1000米,格式化的距离总是以码或米(取决于语言环境)显示。
例如,如果距离= 450.0,格式化的字符串将是492.7码或450米。
如何调整NSLengthFormatter以仅返回英里/公里的距离字符串?
你有没有试过设置一个不同的'numberFormatter'并且使用[NSNumberFormatter]的不同属性(https:/ /developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNumberFormatter_Class/index.html#//apple_ref/doc/c_ref/NSNumberFormatter) – luk2302
是的,我做过。我曾尝试设置小数位数和舍入模式的数量,但没有对测量输出产生影响。我仍在浏览房产,但希望有人已经实现了同样的目标。 – ZeMoon
请参阅[这个答案](http://stackoverflow.com/questions/2324125/objective-c-string-formatter-for-distances)。 – Droppy