databasemodeling

How do you model a table for unit conversion?


I'm looking to create a db model of various units and their relation to each other. For instance, 36 inches = 3 feet = 1 yard = .9144 meters etc. This table would also store cups in ounces, pounds, kg, grams, cm and all sorts of measurements.

How do you do this? I was thinking about something like this:

Amount | Units | ConversionFactor | ConversionUnits
1      | foot  | 12               | inches
1      | yard  | 36               | inches

But frankly, this seems like a terrible idea. Trying to figure out how many feet in a yard would be very convoluted, and I don't think I could ever store all the conversions I need.

What other ideas are there? I know this is a solved problem. Thanks!


Solution

  • Store conversions to SI units, not to other non-metric units. Then you can convert between units in without needing to know the explicit conversion.

    Unit         | Class      | Base Unit Multiplier
    ------------------------------------------------------
    foot         | length     | 0.304800610
    yard         | length     | 0.914401830
    square foot  | area       | 0.092903040
    ...
    

    So 14 feet in yards is:

    14 feet * 0.304800610 = 4.26720854 meters
    4.26720854 meters * 0.914401830⁻¹ = 4.66666667 yards