Reputation: 4757
is there a .NET library that can perform a numerical operation and return a value?
I have an expression like so:
1 + 1/2
this should return the double equivalent of the same. I wont be passing a string, it will be a numeric value and the return should be a numeric.
Upvotes: 1
Views: 443
Reputation: 30902
I've used NCalc for medium-to-complex calculations and it seems that it will fit your needs.
About the OP's comment on the original question:
double d = 1/2;
will return 0 in c#, because the integer 1 is divided by the integer 2, resulting in an integer result of 0.
If you need to trigger real-number mathematics, at least one of the operands must be defined as a real number. You can do that by specifying it with a decimal point (1.0 instead of 1), or by adding a type specifier after the value (1f or 1d instead of 1). Take a look at this example:
double d1 = (1 + 1/2); //returns 1
double d2 = (1 + 1.0/2); //returns 1.5
double d3 = (1 + 1/2.0); //returns 1.5
double d4 = (1 + 1f/2); //returns 1.5
double d5 = (1 + 1d/2); //returns 1.5
Upvotes: 2
Reputation: 10712
See here CodeDom Calculator - Evaluating C# Math Expressions Dynamically
Upvotes: 0