namespace CSMic.StandardLibrary.Functions.Rounding
{
///
/// Represents the standard-library round function.
///
///
/// The round function evaluates a numeric value and rounds it to the requested precision.
///
public class Round : FunctionBase, ICodedFunction
{
///
/// Gets the expression-language name used to invoke this function.
///
/// round.
public string Name
{
get
{
return "round";
}
}
///
/// Gets the argument signature expected by the round function.
///
/// Two numeric arguments named value and precision.
public override IEnumerable ExpectedArguments
{
get
{
yield return new FunctionArgument("value", FunctionValue.NUMBER);
yield return new FunctionArgument("precision", FunctionValue.NUMBER);
}
}
///
/// Executes the round function.
///
///
/// The evaluated arguments supplied to the function. Exactly two numeric arguments are expected.
///
///
/// A numeric containing the input value rounded to the requested precision.
///
public FunctionValue Execute(params FunctionArgument[] args)
{
return Execute(args, (_args) =>
{
var inputValue = _args[0].Value;
decimal value = Convert.ToDecimal(inputValue.Value);
var inputPrecision = _args[1].Value;
decimal precision = Convert.ToDecimal(inputPrecision.Value);
precision = Math.Round(precision);
int precisionInt = Convert.ToInt32(precision);
return new FunctionValue(FunctionValueType.Numeric, Math.Round(value, precisionInt));
});
}
}
}