Question

I am trying to evaluate a math string and googled it a lot. I found a code snippet but didn't work for me. Here is the code

static decimal evaluate(string expression)
{
        var loDataTable = new DataTable();
         // ******* throws "The expression has a syntax error" message ******
        var loDataColumn = new DataColumn("Eval", typeof(double), expression);
        loDataTable.Columns.Add(loDataColumn);
        loDataTable.Rows.Add(0);
        return (decimal)(loDataTable.Rows[0]["Eval"]);
}

I am passing ((159,00)*(1,0)) as a string and throws

The expression has a syntax error

exception on the second line.

What do you think the problem is? thanks

Was it helpful?

Solution 2

You need to use . instead of , ( := ((159.00)*(1.0))

Oh, on a side node:

You need to invoke DataTable.compute() to invoke a evaluation of the expression:

 static decimal evaluate(string expression)
 {
    var loDataTable = new DataTable();
    return (decimal)(loDataTable.Compute(expression, ""));
 }

OTHER TIPS

try this:

 public static void Main()
 {
    string expression = "((159,00)*(1,0))";
    decimal result = evaluate(expression.Replace(".","").Replace(",","."));
 }
 static decimal evaluate(string expression)
 {
    var loDataTable = new DataTable();
    var loDataColumn = new DataColumn("Eval", typeof(double), expression);
    loDataTable.Columns.Add(loDataColumn);
    loDataTable.Rows.Add(0);
    return decimal.Parse(loDataTable.Rows[0]["Eval"].ToString());
 }

as I understand European notation, commas and periods are switched. So first remove any periods, then replace commas with periods using this:

expression.Replace(".","").Replace(",",".")

(to ensure a string like ((159.162,00)*(2,0)) doesn't break)

Also, note the return is changed to decimal.Parse(), and it works well

First I thought maybe you need to set the culture so that it expects , not . as decimal separator.

But from here: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

All literal expressions must be expressed in the invariant culture locale. When DataSet parses and converts literal expressions, it always uses the invariant culture, not the current culture.

So, you must convert, but it may not be as simple as ,->., do you have and thousand separators in your culture? That needs to be changed also.

You should use dot instead of comma:

expression.Replace(",",".")

Maybe you can use/develop a parser like that

      string m_str = "((159,00)*(1,0))";
    List<string> m_newList = new List<string>();
    for (int i = 0; i < m_str.Length; i++)
    {
        if (m_str[i].ToString() == ",")
        {
            m_newList.Add(".");
        }
        else
        {
            m_newList.Add(m_str[i].ToString());
        }
    }

or use below code as you see many times in this page.

             expression.Replace(",",".")

Actually, you can parse to a decimal dependant on culture. This way, you don't need all the replacing comma's and spaces. I believe you're turkish? then the below line will suit your needs.

decimal d = decimal.Parse(input, new CultureInfo("tr"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top