Expressive and regional settings.
Closed this issue · 3 comments
Hello !
I am testing my stuff on a test customer machine where regional settings are set with a decimal symbol to "," and a grouping symbol to "."
I assume the formula used by Expressive have to respect invariant culture anyway, I mean, using a dot "." as a decimal separator.
Unfortunately, a formula evaluation gives erroneous results when ran on a machine where regional settings are set another way. For example:
"1 + 2.34" => 235, instead of 3.34.
However,
"1 + 2,34" fails with an unrecognized token.
Well, it is a customer in Germany. I cannot change its regional settings, otherwise he will complain for sure. So I managed to overcome this a funny way by using actual fractions : "1 + 234 / 100" 👍
I suspect a group of .TryParse( to be responsible of that in the CompileExpression method:
if (int.TryParse(currentToken.CurrentToken, out var intValue)) { leftHandSide = new ConstantValueExpression(intValue); } else if (decimal.TryParse(currentToken.CurrentToken, out var decimalValue)) {...
In fact, .TryParse relies on the current regional settings. I suggest to force Invariant Culsture by passing it as a parameter.
What do you think?
Good find on the bug and also thank you for helping to find the cause. I haven’t looked at the code in Visual Studio yet but your suggestion looks spot on!
I really need to release a new version soon but I will make sure I get a fix for this before then.
It’s a difficult subject dealing with locales which is why I originally switched to using InvariantCulture when parsing but obviously missed it here.
Just to confirm the following unit test reproduces the issue:
[TestMethod]
public void ShouldIgnoreCurrentCulture()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
var invariantExpression = new Expression("1 + 2.34", ExpressiveOptions.None);
Assert.AreEqual(3.34, invariantExpression.Evaluate()); // Returns 235
}