measurement converter for selecting unit

2.6k Views Asked by At

I'm seeing quite a few measurement conversion relating gems, but I haven't been able to find one that will select the best/closest unit.

For example

If I give the gem the measurement of

9 inches + 6 inches

I'm trying to get the result

1 foot, 3 inches

The conversion tools I've seen, I'd have to tell the convertor to try to convert to feet, and then decide which is the most appropriate measurement.

1

There are 1 best solutions below

0
On

Not sure how complicated you wanted to get but for your example I did this:

def plain_english_conversion(inches)
    divmod_output = inches.divmod(12)
    puts "#{divmod_output[0]} ft, #{divmod_output[1]} in"
end

puts "15 "
plain_english_conversion(15)

puts "37 "
plain_english_conversion(37)

With the output of:

15

1 ft, 3 in

37

3 ft, 1 in

Certainly I restricted it to feet/inches but you can abstract it out if necessary (inches & yards, feet & yards, etc.)