Way back in my earlier days of programming, I wrote a unit convertor that converted between different units such as distance and weights. It was a really ugly and inelegant solution, so I thought I would look at coming up with a new and improved version.

Unit conversions themselves are very easy to accomplish. All you need is a reference unit (which for this tutorial, we will use metres) and the conversion amount between the two units you are converting between and the reference unit.

So, to put that simpler, if you want to convert from millimetres to kilometres, you first convert the millimetres to metres, and then convert the metres value to kilometres in a two-part calculation.

The trick though is not the calculation itself, but rather managing the conversion rules.

The first thing we are going to do is create an enumeration of all the units we want to convert, which will make referencing an array of conversion values easier later.

namespace ConversionLib
    public enum DistanceUnit

Next up, we create a class which will contain the code todo the conversion.

namespace ConversionLib
    public class Distance

Inside this class, we need to declare two arrays – one to contain the converstion ratios and a second to contain the symbols for each of the units. These are declared as static so that we can use them without instantiating the class.

The second array is not a requirement, but is very useful to display the values.

public static double[] Conversions = new double[]
            1,                 //Metre
            10,                //Decimetre
            100,               //Centimetre
            1000,              //Millimetre
            0.001,             //Kilometre
            3.280839895,       //Feet
            39.37007874,       //Inches
            0.00020712331461,  //Leagues
            0.00017998560115,  //Nautical leagues
            39370078.74,       //Microinches
            0.00062137119224,  //Miles
            1.0936132983       //Yards

public static string[] Symbols = new string[]
        " leagues",
        " naut. leagues",
        " yards"

All that is left is to write a function to actually do the two-part calculation of the conversion.

The function takes the from and to unit parameters as the enumerator we declared, and then decides which values to use in the conversion

public static double Convert(double fromValue, DistanceUnit fromUnit, DistanceUnit toUnit)
    double workingValue;

    if (fromUnit == DistanceUnit.Metres)
        workingValue = fromValue;
        workingValue = fromValue/Conversions[(int) fromUnit];

    if (toUnit != DistanceUnit.Metres)
        workingValue = workingValue*Conversions[(int) toUnit];

    return workingValue;

Now you can just call this function from your code, and it will spit out the converted value for you.

In this example, I have only used a few units, but the code is easily expandable to include any units which you feel like, and can also be easily modified to calculate units other than distance, such as weight or volume, simply by changing the array of units.