Objective c string formatter for distances

后端 未结 7 1833
借酒劲吻你
借酒劲吻你 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:28

    NSLengthFormatter which was introduced with iOS 8 and OS X 10.10 is an option people should be aware of.

        NSLengthFormatter *lengthFormatter = [[NSLengthFormatter alloc] init];
        lengthFormatter.unitStyle = NSFormattingUnitStyleShort;
    
        NSLog(@"distance = %@", [lengthFormatter stringFromMeters:meters]);
    

    However, NSLengthFormatter has doesn't use Imperial units in those locales where they use metric except for distances.

提交回复
热议问题