9

Is there a method that allows me to evaluate a mathematical expression in a string? Example (Not actual Code):

Input = "2+2"

Output = SomeMethod(Input)

Output = 4

Update: Nevermind, I found a way around it by using DataTable.Compute.

Walker
  • 4,879
  • 3
  • 20
  • 16

1 Answers1

3

You'll need a math expression parser to handle this.

Here are some various open source options on CodePlex:

A search will find many others...

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks for the input but are there any libraries in the .Net that can do this? I'd prefer not to use an external parser...I don't need anything complex as long as it can add, subtract, multiply, and divide. – Walker Jul 07 '11 at 01:32
  • @Leonardo: Not directly. You can use CodeDom to generate this yourself, but it's a fair amount of work... – Reed Copsey Jul 07 '11 at 01:52
  • Hmmm I think I'll use CodeDom instead then, thanks for the help – Walker Jul 07 '11 at 02:02