Objective c string formatter for distances

Posted by nevan on Stack Overflow See other posts from Stack Overflow or by nevan
Published on 2010-02-24T06:32:49Z Indexed on 2010/04/18 1:23 UTC
Read the original article Hit count: 337

Filed under:
|
|
|
|

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 number nicely. Converting to miles would be a bonus. I'm sure many people have had a need for one of these and I'm hoping that there's some code floating around somewhere.

Here's how I'd like the formats:

  • 0-100m: 47m (as a whole number)
  • 100-1000m: 325m or 320m (round to the nearest 5 or 10 meters)
  • 1000-10000m: 1.2km (round to nearest with one decimal place)
  • 10000m +: 21km

If there's no code available, how can I write my own formatter?

Thanks

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa