package commons.math.formulas;
public class PercentOfValueWithLimits implements Formula
{

/**
 * Calculates a percentage of an input value and limits the result to a given range. arg[0] : input
 * value arg[1] : percent arg[2] : low limit arg[3] : high limit If high limit > low limit it is
 * ignored (like not set).
 */
public double calculate (double... arg)
{
   double result = 0D;
   double value = arg[0];
   double percent = arg[1];
   double lowest = arg[2];
   double highest = arg[3];
   result = value / 100 * percent;
   if (arg.length >= 3) result = result < lowest ? lowest : result;
   if (arg.length >= 4) result = result > highest ? highest : result;
   return result;
}
}
