feet and inches
a guest Jul 2nd, 2011 121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- from math import floor
- from fractions import Fraction
- def convert(n):
- feet, r = int(floor(n) / 12), n % 12
- inches = int(floor(r))
- numer = int(round(32 * (r - inches)))
- return feet, inches, Fraction(numer, 32)
- def show_inches(inches, fractions):
- def pluralize_inches(i):
- if i == 1:
- return str(i) + " inch"
- return str(i) + " inches"
- if inches and fractions:
- return str(inches) + ' and ' + str(fractions) + ' inches'
- elif inches or fractions:
- return pluralize_inches(inches or fractions)
- else: return ''
- def feet_and_inches(n):
- def pluralize_feet(ft):
- if not ft:
- return ''
- elif ft == 1:
- return str(ft) + ' foot'
- return str(ft) + ' feet'
- if not n:
- return "0 feet 0 inches"
- feet, inches, fractions = convert(n)
- ft = pluralize_feet(feet)
- i = show_inches(inches, fractions)
- return ft + (ft and i and ' ') + i
RAW Paste Data