This question already has answers here:
Is there a string math evaluator in .NET?
(18 answers)
Closed 9 years ago.
Say a program receives an input string "8*10+6/2" and should output 83, in this case. how to handle the operator?
I can chop the string into individual strings, then detect whether it is a number or operator. If it is an operator I can convert it to int. But I have no idea how to handle the operator so that the calculation works.
You could use the DataTable.Compute-"trick":
double result = (double)new DataTable().Compute("8*10+6/2", null);
The following arithmetic operators are supported in expressions:
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
More informations in: DataColumn.Expression at Expression Syntax.
Related
This question already has answers here:
Is there a string math evaluator in .NET?
(18 answers)
how to convert a string to a mathematical expression programmatically
(6 answers)
Closed 2 years ago.
I want to calculate a string in C# using either NCalc or DynamicExpresso library, the problem is, when the calculation gets complex and the numbers are big, it returns the wrong result.
For example the code below returns -808182895 when it should return 3486784401
string value = "387420489*9";
value = new Interpreter().Eval(value).ToString();
Am i doing anything wrong?
Thanks for the help.
Try the following:
(long)387420489 * (long)9
Dynamic Expresso has a web shell here where you can test the expressions;
http://dynamic-expresso.azurewebsites.net/
While testing on this web shell, I realized that;
387420489L * 9 => Syntax error (at index 9). => does not accept type suffix
(long)387420489 * 9 => -808182895 => overflow
387420489 * (long)9 => 3486784401 => OK
2147483647 + 1 => -2147483648 => int.MaxValue + 1 = int.MinValue (overflow)
2147483648 + 1 => 2147483649 => When does not fit into Int32, interpreted as long
While most of these can be regarded as by design (considering how Dynamic Expresso evaluates the statement), there can still be further improvement.
Think of Javascript for example.
387420489*9 => 3486784401
The question is, is what we need
to execute the given arithmetic expression correctly, as we and the end-user expects,
to execute the given arithmetic expression the C# way?
The former, I think.
This question already has answers here:
question mark inside C# syntax [duplicate]
(7 answers)
How Ternary operator works in C# [duplicate]
(3 answers)
Closed 3 years ago.
Can any one tell me the meaning of the below C# code
true? para1:para2;
For example,
char x = 'A';
Console.WriteLine(true? x : 0);
The console prints 65
I do not understand how it works.
Its a ternary operator
condition? 'execute this part if condition satisfies':'execute this part if condition not satisfies'
In your example Console.WriteLine(true? x : 0);
if something is true,it writes A(as the value of x is A) else 0
This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Is there a string math evaluator in .NET?
(18 answers)
Closed 4 years ago.
I have one scenario in C#, which I fetch the expression from database, I need to parse and evaluate the same
The expression can be of any type (With + & - Operator only) as mentioned below
X+Y-Z
X-Y-Z
Z-Y+Z
Where as at run time I have any of above expression as string item and the values for each variable defined in it.
Now I need to automate the same, so that my code at run time will able to parse and evaluate the same.
I believe Switch case and if/else loop is the one way, but any can please suggest some other better and efficient way.
Thanks in advance
This question already has answers here:
Is there an exponent operator in C#?
(9 answers)
Closed 8 years ago.
I'm doing this equation
double1 * ((double2/double3) ^ 2.333)
getting error
operator '^' cannot be used on type double.
what variable should I use instead of double?
Use Math.Pow(value, exponent) instead.
Math.Pow(3,2) outputs 9
The operator you attempted to use (^) cannot be used for this purpose. It is a logical XOR and should be used for bitwise exclusive OR operations.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert from scientific notation string to float in C#
Is it there an build-in function which converts string in format "2.71e+006" to a number or I have to write my custom algorithm?
The Decimal Parse method has an overload you can use:
decimal d = Decimal.Parse("2.71e+006", System.Globalization.NumberStyles.Float);
You can also do the same for a Double.