问题
I'm trying to run this formula in NCalc:
"( Abs([a] - [b]) / ( ([a] + [b]) / 2.0 ) ) * 100"
I get the error:
Operator '/' can't be applied to operands of types 'decimal' and 'double'
The [a] and [b] parameters are passed as Decimals. I tried putting 'm' on the 2 and 100 like so:
"( Abs([a] - [b]) / ( ([a] + [b]) / 2m ) ) * 100m"
But it throws an exception:
Additional information: extraneous input 'm' expecting ')' at line 1:36
I followed this question, but it didn't help me. The same question posted on codeplex with no answer. Any ideas?
回答1:
Possible workaround is to pass 2m
as parameter to make it recognized properly as a decimal
value, for example :
string strExp = "( Abs([a] - [b]) / ( ([a] + [b]) / [c] ) ) * 100";
Expression e = new Expression(strExp);
e.Parameters["a"] = 3.5m;
e.Parameters["b"] = 1m;
e.Parameters["c"] = 2m; //<- pass 2m as expression parameter
来源:https://stackoverflow.com/questions/26191073/operator-cant-be-applied-to-operands-of-types-decimal-and-double-ncal