Operator '/' can't be applied to operands of types 'decimal' and 'double' - NCalc

↘锁芯ラ 提交于 2019-12-13 04:55:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!