Friday, April 24, 2009

Arithmetic Parser

I was shocked to learn that the process of applying arithmetic order of operations was already available to you in .Net. Sure it can be done through a slightly obscure fashion, but the results are fantastic. Better yet nearly only 4 lines of code...amazing.


string expression = "(2.2 + .8) * 10";
DataTable dataTable = new DataTable();
dataTable.Columns.Add("col1", typeof(double), expression);
dataTable.Rows.Add(new object[] { });
MessageBox.Show(Convert.ToString(dataTable.Rows[0][0]));

Outputs 30....What a time saver.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.