0

I would like to format a height in a human readable way and I want to check I am not reinventing the wheel.

So, say I want to output 5' 1" 1/8

Does the Foundation framework do this out of the box or do I need to roll my own? I'm not asking anyone to do the rolling, just that I'm not missing the intended way of doing this.

import UIKit

let feet = Measurement<UnitLength>(value: 5, unit: .feet)
print(feet)

let inches = Measurement<UnitLength>(value: 1.125, unit: .inches)
print(inches)

let height = (feet + inches).converted(to: .feet)
print(height)

// I want to output 5' 1" 1/16

let formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "en_US")

formatter.unitOptions = .naturalScale

formatter.unitStyle = .long
print(formatter.string(from: height))
formatter.unitStyle = .medium
print(formatter.string(from: height))
formatter.unitStyle = .short
print(formatter.string(from: height))

Output

5.0 ft
1.125 in
5.09375 ft
61.125 inches
61.125 in
61.125″
lewis
  • 2,936
  • 2
  • 37
  • 72
  • 1
    thanks for the feedback @LeoDabus :) I would normally have changed the lets once the compiler nagged me about it, but playgrounds is less naggy. And again I used UnitLength. to make it on the IDE to hint and auto complete. I've removed them from the question to avoid distraction – lewis Feb 14 '19 at 15:44
  • Just a note Xcode should auto complete with just a period – Leo Dabus Feb 14 '19 at 15:48
  • Using the `formatter.unitStyle = .short` will obviously give you what you want for the feet and (whole)inches but I think you may need to extract the fractional/decimal part and construct it manually. Will that part always be exactly divisible into 1? If not then creating a neat fraction would be problematic. – Magnas Feb 14 '19 at 15:55
  • @LeoDabus should being the operative word :) sometimes it does, sometimes it doesn't – lewis Feb 14 '19 at 15:57
  • @Lewis42 I meant in obvious cases like this. But I agree sometimes I do expect to auto complete and it doesn't – Leo Dabus Feb 14 '19 at 16:03
  • @Magnas I had started down that road before I saw your comment, but it turns out that that is problematic, or at least the way I am doing it, because swift thinks that 5 ft 1 inch - 5ft = 0.99999999999 inches – lewis Feb 14 '19 at 16:34
  • This may not fit your situation but if you use inches for your height instead of feet, `var inches = Measurement(value: 1, unit: UnitLength.inches)` and `var height = (feet + inches).converted(to: UnitLength.inches)`: `let remainder = height - Measurement(value: 5, unit: UnitLength.feet)` results in `0.02540000000000009 m` and `let remainderInInches = remainder.converted(to: UnitLength.inches)` results in `1.0000000000000036 in` I'm not sure why `remainder`'s unit is given as `m`. – Magnas Feb 14 '19 at 17:17
  • @Magnas yes that seems better, thank you. in the end I found that working in inches gave more reliable results – lewis Feb 14 '19 at 17:33
  • https://stackoverflow.com/a/35895607/2303865 – Leo Dabus Feb 15 '19 at 16:12

0 Answers0