Decimal parameters and float constants

Topics: General, Help
Feb 4, 2014 at 11:25 PM
I know there is a similar discussion on this topic that indicates this issue is fixed, but with the current version of NCalc (1.3.8) I am still having trouble when using a Decimal parameter. For example. this code

NCalc.Expression testExpression = new NCalc.Expression("Input * 1.1");
testExpression.Parameters.Add("Input", (decimal)1.5);
object results = testExpression.Evaluate();

Fails with exception, "Operator '*' can't be applied to operands of types 'decimal' and 'double'"

Is there any way to use a Decimal parameter in combination with a constant in expression that happens to also be a floating-point value?
Jan 20, 2016 at 12:26 PM
Edited Jan 20, 2016 at 1:28 PM
I have the exact same issue!

(X + Y) * 2.23

will fail, if you pass X and Y as decimal parameters!


My fix is:
Because NCalc assumes the constants to be Double, we have no choice but pass the parameters as Double too. Otherwise you have a mix of decimal & double, which fails. So basically, pass the parameters as double and then Convert.ToDecimal(...) your result.