faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 17 people (59%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Metric conversions

Jul 5th, 2000 10:01
Nathan Wallace, Hans Nowak, Snippet 220, Python Snippet Support Team


"""
Packages: miscellaneous.mundane
"""
# metric.py
""" Metric conversions.
    Derived from:
    "ANSI C Metric Conversion Macros", by Mike Smith.
"""
""" Length conversion functions """
def cm_to_inch(x): return 0.3937 * x
def inch_to_cm(x): return 2.54 * x
def inch_to_m(x): return 0.0254 * x
def inch_to_mm(x): return 25.4 * x
def km_to_mile(x): return 0.6214 * x
def m_to_inch(x): return 39.37 * x
def m_to_yard(x): return 1.093611 * x
def mile_to_km(x): return 1.609 * x
def mm_to_inch(x): return 0.03937 * x
def yard_to_m(x): return 0.9144 * x
def inch_to_foot(x): return x / 12.0
def inch_to_yard(x): return x / 36.0
def inch_to_mile(x): return x / 63360.0
def foot_to_inch(x): return 12.0 * x
def foot_to_yard(x): return x / 3.0
def foot_to_mile(x): return x / 5280.0
def mile_to_inch(x): return 63360.0 * x
def mile_to_foot(x): return 5280.0 * x
def mile_to_yard(x): return 1760.0 * x
""" Weight conversion """
def gram_to_ounce(x): return 0.03527 * x
def kg_to_lbs(x): return 2.2046 * x
def lbs_to_kg(x): return 0.4545 * x
def lbs_to_ounce(x): return 16 * x
def ounce_to_gram(x): return 28.3527 * x
def ounce_to_lbs(x): return x / 16.0
""" Liquid conversion """
def cl_to_ounce(x): return 0.338 * x
def ounce_to_cl(x): return 2.95857988 * x
# There are 16 ounces in a US quart, but 20 ounces in a UK quart! 
def liter_to_ukgallon(x): return 0.2199 * x
def liter_to_ukquart(x): return 0.945 * x
def liter_to_usgallon(x): return 0.26455 * x
def liter_to_usquart(x): return 1.0582 * x
def usgallon_to_liter(x): return 3.78 * x
def usquart_to_liter(x): return 0.945 * x
def usgallon_to_ukgallon(x): return 0.8 * x
# Four quarts in a gallon:
def quart_to_gallon(x): return x / 4.0
# UK/US gallon ambiguity solved:
def usquart_to_ounce(x): return x * 16.0
def ukquart_to_ounce(x): return x * 20.0
def usgallon_to_ounce(x): return usquart_to_ounce(x) * 4.0
def ukgallon_to_ounce(x): return ukquart_to_ounce(x) * 4.0
# Pints (courtesy of Jennifer McSparin: ;-)
# A gallon is 2 pints.
def gallon_to_pint(x): return x / 2.0
""" Area conversion """
def cm2_to_inch2(x): return 0.155 * x
def foot2_to_m2(x): return 0.0930 * x
def inch2_to_cm2(x): return 6.4516 * x
def km2_to_mile2(x): return 0.386 * x
def m2_to_foot2(x): return 10.75 * x
def mile2_to_km2(x): return 2.5906 * x
""" Volume conversion """
def cm3_to_inch3(x): return 0.061 * x
def inch3_to_cm3(x): return 16.3934 * x
def km3_to_mile3(x): return 0.25 * x        # who will ever need these? :)
def m3_to_yard3(x): return 1.308 * x
if __name__ == "__main__":
    pass