'Use DataTable Compute method for a different locale?

I'm using the Compute method to evaluate an expression. My current locale is de-DE. This is simply done by:

var dt = new DataTable() {
    Locale = CultureInfo.CurrentCulture,
};

var v = dt.Compute(myExpression);

When I enter an expression such as:

30.5 / 2.25

The expression is evaluated correctly.

However, when as my locale is different, I need to enter the above expression as:

30,5 / 2,25

The expression is not evaluated as expected.

How do I get the Compute method to evaluate expression correctly for the given locale?



Solution 1:[1]

Easy, crude workaround that works:

var dt = new DataTable();

var temp = myExpression.Replace(".", string.Empty).Replace(",", ".");
var v = dt.Compute(temp, string.Empty);

Won't work when changing locales. Might be expanded to handle locales.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ivan-Mark Debono