目标c字符串格式化距离

nev*_*ing 10 string cocoa distance objective-c formatter

我有一个漂浮的距离,我正在寻找一种方法来为人类读者很好地格式化它.理想情况下,我希望它随着它变大而从m变为km,并且很好地将数字四舍五入.转换为里程将是一个奖励.我相信很多人都需要其中一个,我希望有一些代码可以在某个地方浮动.

这是我喜欢的格式:

  • 0-100m:47m(整数)
  • 100-1000米:325米或320米(圆到最近的5或10米)
  • 1000-10000m:1.2km(舍入到最近,小数点后一位)
  • 10000米+:21公里

如果没有可用的代码,我该如何编写自己的格式化程序?

谢谢

ma1*_*w28 25

这些解决方案都没有真正满足我的需求,所以我建立了它们:

#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]);    
}
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢这个解决方案.基于它创建了一个"NSString + Distance"类别.要点:https://gist.github.com/1397837 (3认同)

Kla*_*aas 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]);
Run Code Online (Sandbox Code Playgroud)

更新:

似乎MKDistanceFormatter以某种方式舍入输入值.例如,当我将myDistance设置为111.0时,我得到"100米".


ken*_*ytm 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));
Run Code Online (Sandbox Code Playgroud)