What would be the best way to refactor following methods?private string CalcualtePercentageDifference(string temp1, string temp2)    {        double number1 = ParseDouble(temp1);        double number2 = ParseDouble(temp2);        double percentageDifference = 0;         if (number1 == 0 || number2 == 0)         {             return 0 %;         }        if ((number1 >= number2) && number1 > 0 && number2 > 0)        {            double tempNumber = number2*100/number1;            Math.Abs(percentageDifference = 100 - tempNumber);        }        if ((number2 >= number1) && number1 > 0 && number2 > 0)        {            double tempNumber = number1*100/number2;            Math.Abs(percentageDifference = 100 - tempNumber);        }        if (number1 < 0 && number2 > 0)        {            double tempNumber = (number2 + Math.Abs(number1))/Math.Abs(number1);            Math.Abs(percentageDifference = 100 - tempNumber);        }        if (number2 < 0 && number1 > 0)        {            double tempNumber = (number1 + Math.Abs(number2))/Math.Abs(number2);            Math.Abs(percentageDifference = 100 - tempNumber);        }        if (number1 < 0 && number2 < 0)        {            number1 = Math.Abs(number1);            number2 = Math.Abs(number2);            if ((number1 >= number2))            {                double tempNumber = number2 * 100 / number1;                Math.Abs(percentageDifference = 100 - tempNumber);            }            if ((number2 >= number1))            {                double tempNumber = number1 * 100 / number2;                Math.Abs(percentageDifference = 100 - tempNumber);            }        }        return Math.Round(percentageDifference) +  %;    }    private static double ParseDouble(object value, string valueName = )    {        double outValue = double.NaN;        try        {            outValue = Convert.ToDouble(value);        }        catch (FormatException e)        {            //We will get here when value contains NumberDecimalSeparator different from this in current culture            //Convert now using suitable culture            outValue = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == ,                       ? Convert.ToDouble(value, CultureInfo.InvariantCulture)                       : Convert.ToDouble(value, new CultureInfo(pl-PL));        }        catch (InvalidCastException)        {        }        return outValue;    }
Percentage calculation
c#;calculator
Some general observations first before I go into my refactored proposal:I would try and use a better parameter naming rather than temp1, temp2.  To be honest I'm not sure if this is better but perhaps percentage1, percentage2 at least indicates the value is expected to be a percentage.Try and get spelling correct. CalcualtePercentageDifference should be CalculatePercentageDifferenceConsistent spacing. Sometimes you have spaces after your if statements sometimes you don't. I personally prefer spaces.Math.Abs returns a value. You aren't using that value so the calls to this method make no sense. Either remove it or use the value? For the purpose of this review, I'm going to assume you want to use the value.You have redundant brackets in some of your if statements. Consider removing these.if ((number1 >= number20))If a variable isn't required in a function, then I would suggest you remove it.private static double ParseDouble(object value)ParseDouble potentially returns NaN. I think you can account for that in code without letting general exceptions occur.I'm not sure why you need to swallow the InvalidCastException. In fact, is swallowing the exceptions at all what you are after?In general I try not to do this, but it can depend on the situation, so perhaps in this case it's ok.Now for the code:public string CalculatePercentageDifference(string percentage1, string percentage2){    double number1 = ParseDouble(percentage1);    double number2 = ParseDouble(percentage2);    if (double.IsNaN(number1) || double.IsNaN(number2))    {        return NaN;    }    var percentageDifference = CalculatePercentageDifference(number1, number2);    return percentageDifference +  %;}Parse double without the additional parameter:private static double ParseDouble(object value){    double outValue = double.NaN;    try    {        outValue = Convert.ToDouble(value);    }    catch (FormatException e)    {        //We will get here when value contains NumberDecimalSeparator different from this in current culture        //Convert now using suitable culture        try        {            outValue = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator == ,                       ? Convert.ToDouble(value, CultureInfo.InvariantCulture)                       : Convert.ToDouble(value, new CultureInfo(pl-PL));        }        catch (FormatException)        {            // If we still get here then there's something wrong        }    }    catch (InvalidCastException)    {    }    return outValue;}And the main refactored method:public int CalculatePercentageDifference(double number1, double number2){    if (number1.Equals(0) || number2.Equals(0))    {        return 0;    }    const double tolerance = 0.000000001;                // If numbers are the same then there is no difference    if (Math.Abs(number1 - number2) < tolerance)    {        return 0;    }    Func<double, int> percentageDifference = number =>    {        var difference = Math.Abs(100 - number);        return (int) Math.Round(difference);    };    Func<double, double, int> differenceHighLow = (numberHigh, numberLow) =>    {        var number = numberHigh * 100 / numberLow;        return percentageDifference(number);    };    // Convert to positive numbers if they are both negative    if (number1 < 0 && number2 < 0)    {        number1 = Math.Abs(number1);        number2 = Math.Abs(number2);    }    // At least one of the numbers are positive at this stage    if (number1 > 0 && number2 > 0)    {        return number1 > number2 ?             differenceHighLow(number2, number1) :             differenceHighLow(number1, number2);    }    // One number is a negative but the other is positive    Func<double, double, int> postiveNegativeDifference = (postiveNumber, negativeNumber) =>    {        var tempNumber = (postiveNumber + Math.Abs(negativeNumber)) / Math.Abs(negativeNumber);        return percentageDifference(tempNumber);    };    if (number1 < 0 && number2 > 0)    {        return postiveNegativeDifference(number2, number1);    }    return postiveNegativeDifference(number1, number2);}And of course a refactoring wouldn't be anywhere without some unit tests (of course, more would be recommended):[TestMethod]public void DifferenceBetweenZero_IsZero(){    // Arrange    var model = new _101777_PercentageCalcManyIfs();    // Act    var difference = model.CalculatePercentageDifference(0.0, 0.0);    // Assert    Assert.AreEqual(0 %, difference);}[TestMethod]public void DifferenceBetweenNonNumbers_IsNan(){    // Arrange    var model = new _101777_PercentageCalcManyIfs();    // Act    var difference = model.CalculatePercentageDifference(a.0, 0.0);    // Assert    Assert.AreEqual(NaN, difference);}[TestMethod]public void DifferenceBetweenTwoPostiveNumbers(){    // Arrange    var model = new _101777_PercentageCalcManyIfs();    // Act    var difference = model.CalculatePercentageDifference(5.0, 10.0);    // Assert    Assert.AreEqual(50 %, difference);}