-1

How can I perform square root calculation of Economic Order Quantity using decimal inputs when the default return type of square root is actually a double?

I tried using the method suggested in similar question How can I improve this square root method? but it is not does not share a similar context to my problem. I have tried casting to type decimal but it does not seem to work.

public class Sample
    {
        public int AnnualSalesUnits { get; set; }
        public decimal OrderCost { get; set; }
        public decimal HoldingRate { get; set; }
        public decimal UnitCost { get; set; }
        public decimal EconomicOrderQuantity => Math.Sqrt((2 * OrderCost * AnnualSalesUnits) / ((HoldingRate / 100) * UnitCost));

    }   

Can someone help me rewrite this formula to account for the decimal inputs?

tamosa
  • 77
  • 1
  • 10

1 Answers1

1

You can declare your property EconomicOrderQuantity as follows:

public decimal EconomicOrderQuantity
{
  get 
  {
    decimal numerator = (2 * OrderCost * AnnualSalesUnits * 100);
    decimal denominator = (HoldingRate * UnitCost);
    decimal fraction = Convert.ToDecimal(numerator / denominator);
    double final = Math.Sqrt(Convert.ToDouble(fraction));
    return Convert.ToDecimal(final);
  }
}

You need to perform some conversion prior to ensure that the proper data types are allowed to perform the calculations required.

Note that I have also rewritten the mathematical formula in a more linear way to provide a better understanding of the flow of the calculations.

Angelo
  • 1,578
  • 3
  • 20
  • 39