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.
Related
This question already has answers here:
Why does Math.Round(2.5) return 2 instead of 3?
(15 answers)
Closed 2 years ago.
I am using VS2008 c# , windows mobile 6.5, so I have to use the .Net Compact Framework, the problem is:
When Math.Round(66.05,1) the result is 66 when the correc value should be 66.1
what can I do ? how should I use the round() function
Thanks
You should use the overloaded static Math.Round method which takes two arguments:
decimal x = Math.Round(2.5555, 2); // x == 2.56
You can read more here.
The issue with this is that the Mathf.Round method by default use "ToEven convention" There is an example using MidpointRounding.AwayFromZero that will give you the result you expect
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:
Find character with most occurrences in string?
(12 answers)
Closed 7 years ago.
I'm trying to find a clever way using linq to examine an IEnumerable and find the max occurrences of some element.
"aba".SomeLinqExpression(); // => 'a'
or
(new List<int>{1, 2, 3, 4, 1, 2, 1}).SomLinqExpression(); // => 1
Is there an easy built in way to do this I think I need an aggregation query but Aggregate and Count don't seem to do it. Maybe groupby?
EDIT: Clarification
I'm looking to get access to the most often seen value. I don't care how ties are handled.
if we have a string
"abcda" // note that there are 2 'a' characters.
Since 'a' is the most common thing in the sequence I want this operation to return 'a'
"abcda".SomeLinqExpression(); // returns 'a'
Well, I guess this will work:
"aqejqkndaasaamd".GroupBy(x => x).OrderByDescending(g => g.Count()).First().Key
This question already has answers here:
C# simple divide problem
(9 answers)
Closed 7 years ago.
This mabey is a dumb question but i try to count discount on a price like this:
newAllaVaror.Pris = System.Convert.ToDouble(
(1 - (clientKampanj.VisaKampanj(vara.ProduktNamn) / 100)) * vara.Pris
).ToString();
It will look like this in reality (1-(20/100)*7.99), but my output is now 7.99 but it should be 6,392..becuse the orginal price is 7.99...
I have tried to move the parentheses but i only get 1 then..
In wich order does C# go thru the parentheses, becuse this should work right??
I'm pretty sure you are doing an integer division here:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100)
So if you have 20/100, it will result in 0 instead of the expected 0.2 as the reminder is truncated.
You need to convert to double, one of the operands:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100.0)
Since #Scott pointed out your other variables are of type decimal, you need to convert one of the operands to that type instead:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100.0m)
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.