我有一个漂浮的距离,我正在寻找一种方法来为人类读者很好地格式化它。理想情况下,我希望它随着它变大而从m变为km,并且很好地将数字四舍五入。转换为里程将是一个奖励。我相信很多人都需要其中一个,我希望有一些代码可以在某个地方浮动。
以下是我喜欢的格式:
如果没有可用的代码,我该如何编写自己的格式化程序?
由于
答案 0 :(得分:24)
这些解决方案都没有真正符合我的要求,所以我建立了它们:
#define METERS_TO_FEET 3.2808399
#define METERS_TO_MILES 0.000621371192
#define METERS_CUTOFF 1000
#define FEET_CUTOFF 3281
#define FEET_IN_MILES 5280
- (NSString *)stringWithDistance:(double)distance {
BOOL isMetric = [[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue];
NSString *format;
if (isMetric) {
if (distance < METERS_CUTOFF) {
format = @"%@ metres";
} else {
format = @"%@ km";
distance = distance / 1000;
}
} else { // assume Imperial / U.S.
distance = distance * METERS_TO_FEET;
if (distance < FEET_CUTOFF) {
format = @"%@ feet";
} else {
format = @"%@ miles";
distance = distance / FEET_IN_MILES;
}
}
return [NSString stringWithFormat:format, [self stringWithDouble:distance]];
}
// Return a string of the number to one decimal place and with commas & periods based on the locale.
- (NSString *)stringWithDouble:(double)value {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setLocale:[NSLocale currentLocale]];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setMaximumFractionDigits:1];
return [numberFormatter stringFromNumber:[NSNumber numberWithDouble:value]];
}
- (void)viewDidLoad {
[super viewDidLoad];
double distance = 5434.45;
NSLog(@"%f meters is %@", distance, [self stringWithDistance:distance]);
distance = 543.45;
NSLog(@"%f meters is %@", distance, [self stringWithDistance:distance]);
distance = 234234.45;
NSLog(@"%f meters is %@", distance, [self stringWithDistance:distance]);
}
答案 1 :(得分:23)
iOS 7和OS X 10.9引入了MKDistanceFormatter来格式化距离:
代码示例:
double myDistance = 21837.0f;
MKDistanceFormatter *df = [[MKDistanceFormatter alloc]init];
df.unitStyle = MKDistanceFormatterUnitStyleAbbreviated;
NSLog(@"myDistance is %@", [df stringFromDistance: myDistance]);
<强>更新强>
似乎MKDistanceFormatter以某种方式舍入输入值。例如。当我将myDistance设置为111.0时,我得到“100米”。
答案 2 :(得分:17)
是的,您需要编写自己的格式化程序,例如
#include <math.h>
NSString* convertDistanceToString(float distance) {
if (distance < 100)
return [NSString stringWithFormat:@"%g m", roundf(distance)];
else if (distance < 1000)
return [NSString stringWithFormat:@"%g m", roundf(distance/5)*5];
else if (distance < 10000)
return [NSString stringWithFormat:@"%g km", roundf(distance/100)/10];
else
return [NSString stringWithFormat:@"%g km", roundf(distance/1000)];
}
...
NSLog(@"The distance is %@", convertDistanceToString(1024));
答案 3 :(得分:4)
NSLengthFormatter
是人们应该注意的选项。
NSLengthFormatter *lengthFormatter = [[NSLengthFormatter alloc] init];
lengthFormatter.unitStyle = NSFormattingUnitStyleShort;
NSLog(@"distance = %@", [lengthFormatter stringFromMeters:meters]);
但是,NSLengthFormatter
在使用指标的区域设置中不使用英制单位,除了距离。
答案 4 :(得分:2)
我是这样做的。这使用用户的语言环境来正确格式化字符串,您也应该这样做。
// Returns a string representing the distance in the correct units.
// If distance is greater than convert max in feet or meters,
// the distance in miles or km is returned instead
NSString* getDistString(float distance, int convertMax, BOOL includeUnit) {
NSString * unitName;
if (METRIC) {
unitName = @"m";
if (convertMax != -1 && distance > convertMax) {
unitName = @"km";
distance = distance / 1000;
}
} else {
unitName = @"ft";
if (convertMax != -1 && distance > convertMax) {
unitName = @"mi";
distance = distance / 5280;
}
distance = metersToFeet(distance);
}
if (includeUnit) return [NSString stringWithFormat:@"%@ %@", formatDecimal_1(distance), unitName];
return formatDecimal_1(distance);
}
// returns a string if the number with one decimal place of precision
// sets the style (commas or periods) based on the locale
NSString * formatDecimal_1(float num) {
static NSNumberFormatter *numFormatter;
if (!numFormatter) {
numFormatter = [[[NSNumberFormatter alloc] init] retain];
[numFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numFormatter setLocale:[NSLocale currentLocale]];
[numFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numFormatter setMaximumFractionDigits:1];
[numFormatter setMinimumFractionDigits:1];
}
return [numFormatter stringFromNumber:F(num)];
}
答案 5 :(得分:0)
今天发现这个问同样的问题....跟着:
NSString *rvalue; if (value > 1000) { rvalue = [NSString stringWithFormat:@"%.02f km",value]; }else { rvalue = [NSString stringWithFormat:@"%.02f m",value]; }如果需要,
可以将其包装在一个方法中
答案 6 :(得分:-1)
对于那些寻找swift 2.0版本的人。来自@MattDiPasquale
extension Double {
func toDistanceString() -> String {
let METERS_TO_FEET = 3.2808399
let METERS_CUTOFF = 1000.0
let FEET_CUTOFF = 3281.0
let FEET_IN_MILES = 5280.0
let format:String
if (NSLocale.isMetric()) {
if (self < METERS_CUTOFF) {
format = "\(self.stringWithDecimals(0)) metres"
} else {
format = "\((self / 1000).stringWithDecimals(1)) km";
}
} else { // assume Imperial / U.S.
let feet = self * METERS_TO_FEET;
if (feet < FEET_CUTOFF) {
format = "\(feet.stringWithDecimals(0)) feet";
} else {
format = "\((self / FEET_IN_MILES).stringWithDecimals(1)) miles";
}
}
return format
}
func stringWithDecimals(decimals:Int) -> String {
return String(format: "%.\(decimals)f", self)
}
}
extension NSLocale {
class func isMetric() -> Bool {
let locale = NSLocale.currentLocale()
return locale.objectForKey(NSLocaleUsesMetricSystem) as! Bool
}
}