Decimal and double values

Topics: Announcements, General, Help
Feb 29, 2012 at 11:46 AM

Hi,

I was getting error while evaluating this expression

var eif = new Expression("1.8 + Abs([var1])");
eif.Parameters["var1"] = 9.2;

Operator '+' can't be applied to operands of types 'decimal' and 'double'

I made some change in Numbers.cs file, like this;

// case TypeCode.Decimal: throw new InvalidOperationException("Operator '+' can't be applied to operands of types 'double' and 'decimal'");
case TypeCode.Decimal: return (Double)a + Convert.ToDouble(b);

and

// case TypeCode.Double: throw new InvalidOperationException("Operator '+' can't be applied to operands of types 'decimal' and 'double'");
case TypeCode.Double: return (Decimal)a + new Decimal((double)b);

It worked for me..

Serkan

Mar 15, 2012 at 12:18 AM

Thanks! You saved my life :) without this my current project just wouldn't have worked.

Do this for all the operators and NCalc rocks again!

Coordinator
Mar 15, 2012 at 12:56 AM

I just pushed the fix on the source code. Not as an official release yet, but you can compile the assembly for your needs. I also upgraded the solution to VS 2010.

Mar 23, 2012 at 9:05 AM

The code I downloaded yesterday only had the fix applied to the addition operator. The following code should fix the subtraction, muliplication and division operators:

 

// case TypeCode.Decimal: throw new InvalidOperationException("Operator '-' can't be applied to operands of types 'double' and 'decimal'");
case TypeCode.Decimal: return (Double)a - Convert.ToDouble(b);

...

// case TypeCode.Double: throw new InvalidOperationException("Operator '-' can't be applied to operands of types 'decimal' and 'double'");
case TypeCode.Double: return (Decimal)a - new Decimal((double)b);

 

// case TypeCode.Decimal: throw new InvalidOperationException("Operator '*' can't be applied to operands of types 'double' and 'decimal'");
case TypeCode.Decimal: return (Double)a * Convert.ToDouble(b);

...

// case TypeCode.Double: throw new InvalidOperationException("Operator '*' can't be applied to operands of types 'decimal' and 'double'");
case TypeCode.Double: return (Decimal)a * new Decimal((double)b);

 

// case TypeCode.Decimal: throw new InvalidOperationException("Operator '/' can't be applied to operands of types 'double' and 'decimal'");
case TypeCode.Decimal: return (Double)a / Convert.ToDouble(b);

...

// case TypeCode.Double: throw new InvalidOperationException("Operator '/' can't be applied to operands of types 'decimal' and 'double'");
case TypeCode.Double: return (Decimal)a / new Decimal((double)b);

 

Apr 26, 2012 at 5:52 PM

hello, 

I maked all this changes and i upgraded to vs2010; i want now compile. when i do this:
- i select Release and then F6

i take the NCalc.dll that is in the folder:  ..\NCalc - Sources\Evaluant.Calculator\bin\Release

i take this dll but is not working for me; 

the message error is: 

Could not load file or assembly 'Antlr3.Runtime, Version=3.1.3.22795, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

 

how compile this project?

sorry, my english is not good...

Apr 26, 2012 at 8:30 PM

ok, everything is fine. I added the Antlr3.Runtime.dll in my project and that´s all. 

Sep 12, 2012 at 2:46 AM

I downloaded the code a few weeks ago, and the fixes described in this thread were not included.  I modified Numbers.cs as skokturk and Kildrummy_Tim suggested, and it worked for me.

Then I realized that the code would have worked as it was, if numbers were being parsed from string as decimal, instead of double.  It turns out that I was converting strings to double in my EvaluateParameterHandler.

So, in my case all that was necessary was to stop converting in EvaluateParameterHandler, and let nCalc do it for me.  Then, Numbers.cs worked just fine.

Nov 25, 2013 at 8:30 AM
Hi.

Is this realy fixed in the source code? I just downloaded the code, but it seems to still be a problem?
Nov 25, 2013 at 4:35 PM
NCalc wasn't the problem at the time. It was the way my app was converting value before the NCalc evaluation took place.

However, I eventually decided that I didn't like the way NCalc was deciding the type of my values, sometimes. I did end up modifying the source to allow certain operations (like adding decimal to double) in the Numbers class, and adding types to the list of common types in the EvaluationVisitor class.

I will update my comment on code plex with the change I made.

ghauan <[email removed]> wrote:

From: ghauan

Hi.

Is this realy fixed in the source code? I just downloaded the code, but it seems to still be a problem?
Nov 25, 2013 at 5:38 PM
Here is the change I made in EvaluationVisitor.cs to help NCalc identify the correct type of my parameters. The order in the list is important. NCalc will choose the first type in the list that your value can be converted to.
private static Type[] CommonTypes = new[] { typeof(Int64), typeof(Double), typeof(Boolean),typeof(Decimal), typeof(Int32), typeof(DateTime), typeof(String)  };
In addition, I made multiple changes (like skokturk and Kildrummy_Tim suggested) to Numbers.cs to add various operations that are currently not supported in source.
Dec 5, 2013 at 11:38 AM
Edited Dec 5, 2013 at 12:47 PM
Hi! Thank all for fix and sugest some improvements, was very usefull for me.

I added a new property for NumberFormat on Expression.
With this feature i can set the decimal separator like :
var strExpression = 1 + [StrNumber];

var x = new Expression(strExpression);
x.NumberFormat.NumberDecimalSeparator = ",";

x.Parameters["StrNumber"] = "1,1"; //can be edit.text

var result = x.Evaluate();  //result.ToString() = 2,1



If someone interest, this is the changes :

Numbers.cs
        //add new property
        public static NumberFormatInfo NumberFormat;
        //add constructor
        static Numbers()
        {
            NumberFormat = ( (CultureInfo) CultureInfo.InvariantCulture.Clone() ).NumberFormat;
        }

      //change:
        private static object ConvertIfString(object s)
        {
            if (s is String|| s is char)
            {               
                //return Decimal.Parse(s.ToString());
                return Decimal.Parse(s.ToString(),NumberFormat);
            }
            return s;
        }
SerializationVisitor.cs
//change constructor      
  public SerializationVisitor()
        {
            Result = new StringBuilder();
            //new NumberFormatInfo {NumberDecimalSeparator = "."};
            _numberFormatInfo = Numbers.NumberFormat; 
        }
Expression.cs
    //add property   
     public NumberFormatInfo NumberFormat
        {
            get { return Numbers.NumberFormat; }
            set { Numbers.NumberFormat = value; } 
        }
Tks!
Mar 4, 2014 at 7:30 PM
@sebastienros, when is the next release scheduled?

Is this project dead?

No release since 2011. :(
Coordinator
Mar 4, 2014 at 9:01 PM
I was busy rewriting Jint (https://github.com/sebastienros/jint)
Maybe you should consider it actually as it provides all the features ncalc can do with greater support.
Mar 4, 2014 at 9:52 PM
Edited Mar 4, 2014 at 9:53 PM
Thanks for the input. Can we please get official release on Nuget with minor fixes (such as the one mentioned in this thread). It looks like there are quite some improvements since 2011.

Separately, please consider this request. I am working on parsing color expressions from LESS and SCSS for Intellisense with WebEssentials extension. Since System.Drawing.Color & System.Windows.Media.Color have arithmetic operators defined (for color math), I was hoping to carry out expression evaluation using ncalc. For now, I am muscling up an ugly hack, that is; to convert all three hexadecimal channel values separately, evaluate them using ncalc and concatenate. If we get OOTB support for hexa color evaluation, that would trim out major chunk of code (as one of the major challenges of real-time Intellisense is to make it performant).

Once again thanks for this nice wonderful project. 8-)