Objective c string formatter for distances

后端 未结 7 1811
借酒劲吻你
借酒劲吻你 2021-02-02 01:45

I have a distance as a float and I\'m looking for a way to format it nicely for human readers. Ideally, I\'d like it to change from m to km as it gets bigger, and to round the n

7条回答
  •  日久生厌
    2021-02-02 02:26

    iOS 7 and OS X 10.9 introduced MKDistanceFormatter for formatting distances:

    Code Example:

    double myDistance = 21837.0f;
    
    MKDistanceFormatter *df = [[MKDistanceFormatter alloc]init];
    df.unitStyle = MKDistanceFormatterUnitStyleAbbreviated;
    
    NSLog(@"myDistance is %@", [df stringFromDistance: myDistance]);
    

    Update:

    It seems that MKDistanceFormatter is rounding the input value somehow. E.g. when I set myDistance to 111.0 I get "100 m".

提交回复
热议问题