0

Following string in my input

"(((5292-5325)-(5401/5))/5325)"

I need to calculate this one and wants to get output in float. For that I used string to float conversion but I got exception. how to do that one?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Malathi Mals
  • 73
  • 13
  • 1
    Please show your code – STF Oct 23 '17 at 09:21
  • What exception? What code? – Equalsk Oct 23 '17 at 09:22
  • You can use [wolframalpha.com](https://www.wolframalpha.com/input/?i=(((5292-5325)-(5401%2F5))%2F5325)) if you need to calculate such a thing. If you want to do it in c# code, please share what you got so far and what research you already done to deal with problem. – Renatas M. Oct 23 '17 at 09:24

2 Answers2

6

I use a DataTable for this purpose usually:

DataTable dt = new DataTable();
var result = dt.Compute("(((5292-5325)-(5401/5))/5325)", "");

Or:

double result = (double)dt.Compute("(((5292-5325)-(5401/5))/5325)", "");
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

Can quickly think of 2 ways:

  1. Faster - Use Spring.Net (specially the Spring.Expressions) which has ExpressionEvaluator that might be able to help in this case
  2. Tedious - Make your own code that does the expression parsing & evaluation. Read About Infix Expression evaluation.
Prateek Shrivastava
  • 1,877
  • 1
  • 10
  • 17