views:

306

answers:

3

I am adding a feature to my program in which the user will have the ability to change their unit of measurement at any time, and have the program recalculate their input and output.

If the user inputs say, 20lbs for an item, then decides he wants to work in kilograms instead, he can select an option to do so at any time, and the program will recalculate his 20lb input to 9Kg. Then if he decides he'd rather work in ounces, it would convert that 9Kg to 320oz, so on and so forth.

What would be the most effective and efficient way to go about this? I've been racking my brain trying to figure out a way to have the correct formula be implemented.

+12  A: 

Do all your calculations in standard units (or the units you prefer to work with).

  • Convert the user's input from whatever they wish into standard units.
  • Perform the calculation using standard units.
  • Convert the answer back into the units the user wants.

Only the user interface needs to care about the unit conversions. The rest of the code does not need to worry about it.

Mark Byers
I agree. Dont convert from the previous unit, you'll loose precision.
James Westgate
...otherwise your lander will either overshoot and fly off into space or crash into the surface in a brilliant fireball! ;)
FrustratedWithFormsDesigner
I would suggest using SI units for the information stored in your database (or whatever you're storing your data in).
msarchet
I had written a test that did this exact thing, but the person whom I am adding this feature for claims he wants to have the code do all the converting (rather than just converting for the interface). Glad to know I wasn't totally wrong :)Thanks a lot for the help guys
EvanRyan
If you do all calculations in standard units, all inputs and outputs have to be converted, causing a loss of precision unless the user happens to be working in the standard unit. If you perform all calculations in the user's working units, you only have to convert if they change units.
Gabe
+4  A: 

Store the value in a known unit of measurement internally (e.g. store everything in kg) and have equations to convert to/from the known unit to all the others.

Basically you only need to convert the units for display, all your internal calculations can be done in a single unit.

As a curve ball, F# has a robust UoM mechanism inbuilt if you fancy something cutting edge to play with...

Paolo
A: 

In a program that I've worked on we had separate classes under a "Units" namespace, such as Units.Torque or Units.Length.

If you create a class like Units.Weight and have all the conversion factors as const values and the possible units as an enum, you can just call that class and convert to the desired units. This is a very basic example of the system we use:

namespace Units
{
    class Weight
    {
        public enum WeightType
        {
            kg,
            lb
        }

        const double kgTolbs = 2.20462262;

        public static double Convert(double value, WeightType fromUnits, WeightType toUnits)
        {
            //Code here to convert units
        }
    }
}

With this structure you can convert to and from any of the unit types at your leisure.

Mike Webb