user2137622
user2137622

Reputation: 3

convert between IMPERIAL & METRIC unit on button click C#

I have this Edit Window Edit Window Imperial. If we click on the Metric Values button all labels should replace the in with mm like Metric,

Need to create a interface/BusinessLogic layer for this. I have this function in utility class. that works fine. But I need to use the same functionality in different screen so need this on each business object.

  public static double UnitConversionCalculation(string strDimension, string strInputUnit, string strOutputUnit, double dblInputValue)
        {

            // This is a primitive conversion utility, we convert to metric units, then convert to output units
            // (think about the number of combinations we avoid by doing the two-step handling)
            double dblWork;

            if (strInputUnit == strOutputUnit)
            {
                dblWork = dblInputValue;
                return (dblInputValue);
            }

            if (strDimension.ToUpper() == "LENGTH")
            {
                // Convert to metric units
                if (strInputUnit.ToUpper() == "FT")
                    dblWork = dblInputValue * 12 * 0.0254;
                else if (strInputUnit.ToUpper() == "IN")
                    dblWork = dblInputValue * 0.0254;
                else if (strInputUnit.ToUpper() == "M" || strInputUnit.ToUpper() == "MT")
                    dblWork = dblInputValue;
                else if (strInputUnit.ToUpper() == "CM")
                    dblWork = dblInputValue / 100;
                else if (strInputUnit.ToUpper() == "MM")
                    dblWork = dblInputValue / 1000;
                else
                {
                    // Unknown input unit, no conversion change
                    dblWork = dblInputValue;
                }


                // Convert to output units
                if (strOutputUnit.ToUpper() == "FT")
                {
                    dblWork = (dblWork / 12 / 0.0254);

                }
                else if (strOutputUnit.ToUpper() == "IN")
                {
                    dblWork = dblWork / 0.0254;
                }
                else if (strOutputUnit.ToUpper() == "M" || strOutputUnit.ToUpper() == "MT")
                {
                }
                else if (strOutputUnit.ToUpper() == "CM")
                {
                    dblWork = dblWork * 100;
                }
                else if (strOutputUnit.ToUpper() == "MM")
                    dblWork = dblWork * 1000;
                else
                {
                    // Unknown output unit, no conversion change
                    dblWork = dblInputValue;
                }
            }
            else if (strDimension.ToUpper() == "VOLUME")
            {
                //convert to SI Units
                if (strInputUnit.ToUpper() == "GAL")
                {
                    dblWork = dblInputValue * 3.7854;
                }
                else if (strInputUnit.ToUpper() == "OZ")
                {
                    dblWork = dblInputValue * 0.02841306;
                }
                else if (strInputUnit.ToUpper() == "CUP")
                {
                    dblWork = dblInputValue * 0.2365875;
                }
                else if (strDimension.ToUpper() == "L")
                {
                    dblWork = dblInputValue;
                }
                else
                {
                    dblWork = dblInputValue;
                }

                //Convert to output units
                if (strOutputUnit.ToUpper() == "L")
                {
                    dblWork = (dblWork * 1);
                }
                else if (strOutputUnit.ToUpper() == "GAL")
                {
                    dblWork = dblWork * 0.2641729;
                }
                else if (strOutputUnit.ToUpper() == "OZ")
                {
                    dblWork = dblWork * 35.19508;
                }
                else if (strOutputUnit.ToUpper() == "CUP")
                {
                    dblWork = dblWork * 4.226766;
                }
            }
            else if (strDimension.ToUpper() == "AREADENSITY")
            {
                if (strInputUnit.ToUpper() == "LB/SQUAREYARD")
                    dblWork = dblInputValue * 453.59237 / 0.8361274;
                else if (strInputUnit.ToUpper() == "G/SQUAREMETER")
                    dblWork = dblInputValue;
                else
                {
                    // Unknown input unit, no conversion change
                    dblWork = dblInputValue;
                }

                // Convert to output units
                if (strOutputUnit.ToUpper() == "LB/SQUAREYARD")
                    dblWork = dblWork / 453.59237 * 0.8361274;
                else if (strOutputUnit.ToUpper() == "G/SQUAREMETER")
                {
                }
                else
                    // Unknown output unit, no conversion change
                    dblWork = dblInputValue;
            }
            else if (strDimension.ToUpper() == "LINEARDENSITY")
            {
                if (strInputUnit.ToUpper() == "OZ/1000INCH")
                    dblWork = dblInputValue * 453.59237 / 25.4 / 16;
                else if (strInputUnit.ToUpper() == "G/METER")
                    dblWork = dblInputValue;
                else
                    // Unknown input unit, no conversion change
                    dblWork = dblInputValue;
                // Convert to output units
                if (strOutputUnit.ToUpper() == "OZ/1000INCH")
                    dblWork = dblWork / 453.59237 * 25.4 * 16;
                else if (strOutputUnit.ToUpper() == "G/METER")
                {
                }
                else
                    // Unknown output unit, no conversion change
                    dblWork = dblInputValue;
            }
            else if (strDimension.ToUpper() == "MASS")
            {
                // Convert to g
                if (strInputUnit.ToUpper() == "LB")
                    dblWork = dblInputValue * 453.59237;
                else if (strInputUnit.ToUpper() == "OZ")
                    dblWork = dblInputValue * 453.59237 / 16;
                else if (strInputUnit.ToUpper() == "KG")
                    dblWork = dblInputValue * 1000;
                else if (strInputUnit == "G")
                    dblWork = dblInputValue;
                else
                    // Unknown input unit, no conversion change
                    dblWork = dblInputValue;

                // Convert from g to output units
                if (strOutputUnit.ToUpper() == "LB")
                    dblWork = dblWork / 453.59237;
                else if (strOutputUnit.ToUpper() == "OZ")
                    dblWork = dblWork / 453.59237 * 16;
                else if (strOutputUnit.ToUpper() == "KG")
                    dblWork = dblWork / 1000;
                else if (strOutputUnit.ToUpper() == "G")
                {
                }
                else
                    // Unknown output unit, no conversion change
                    dblWork = dblInputValue;
            }
            else if (strDimension.ToUpper() == "TEMPERATURE")
            {
                if (strInputUnit.ToUpper() == "F")
                    dblWork = (dblInputValue - 32) / 1.8;
                else if (strInputUnit.ToUpper() == "C")
                    dblWork = dblInputValue;
                else
                    // Unknown input unit, no conversion change
                    dblWork = dblInputValue;
                // Convert to output units
                if (strOutputUnit.ToUpper() == "F")
                    dblWork = dblWork * 1.8 + 32;
                else if (strOutputUnit.ToUpper() == "C")
                {
                }
                else
                    // Unknown output unit, no conversion change
                    dblWork = dblInputValue;
            }
            else if (strDimension.ToUpper() == "PRESSURE")
            {
                if (strInputUnit.ToUpper() == "PSI")
                    dblWork = dblInputValue / 14.696 * 1.01325;
                else if (strInputUnit.ToUpper() == "BAR")
                    dblWork = dblInputValue;
                else
                    // Unknown input unit, no conversion change
                    dblWork = dblInputValue;
                // Convert to output units
                if (strOutputUnit.ToUpper() == "PSI")
                    dblWork = dblWork / 1.01325 * 14.696;
                else if (strOutputUnit.ToUpper() == "BAR")
                {
                }
                else
                    // Unknown output unit, no conversion change
                    dblWork = dblInputValue;
            }
            else if (strDimension.ToUpper() == "FORCE")
            {
                // Convert to Newtons
                if (strInputUnit.ToUpper() == "LBF")
                    dblWork = dblInputValue * 4.448222;
                else if (strInputUnit.ToUpper() == "KGF")
                    dblWork = dblInputValue * 9.80665;
                else
                    // Unknown input unit, no conversion change
                    dblWork = dblInputValue;
                // Convert from Newtons to output units
                if (strOutputUnit.ToUpper() == "LBF")
                    dblWork = dblWork / 4.448222;
                else if (strOutputUnit.ToUpper() == "KGF")
                    dblWork = dblWork / 9.80665;
                else
                    // Unknown output unit, no conversion change
                    dblWork = dblInputValue;
            }
            
            else
            {
                dblWork = dblInputValue;

            }
            // Unknown dimension, no conversion performed


            return dblWork;
        } 

Upvotes: 0

Views: 178

Answers (0)

Related Questions