0

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?

Community
  • 1
  • 1
Liron Harel
  • 10,819
  • 26
  • 118
  • 217

1 Answers1

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
har07
  • 88,338
  • 12
  • 84
  • 137
  • @HenkHolterman Not sure, but I thought C# is acceptable because one question OP followed is about plain C#, not NCalc : "I followed this question...". And `2m` compiled and produced a result for me (didn't check if the calculation result is correct though) – har07 Oct 04 '14 at 08:58