Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for ISNUMBER not recognising numbers without decimal separator (issue #13) #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions Math expression eval/UnitTest/TestAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,6 @@ public void Math_Bitwise_Test()

Expression expr6 = new Expression("BITRSHIFT(13,2)");
Assert.AreEqual(3, expr6.Eval<int>());

Expression expr7 = new Expression("VALUE(\"123\")");
Assert.AreEqual(123, expr7.Eval<int>());

}

[TestMethod]
Expand Down Expand Up @@ -920,6 +916,27 @@ public void Text_Function_IsNumber_Test()
Expression expr1 = new Expression("ISNUMBER(\"0.23\")");
Assert.AreEqual(true, expr1.Eval<bool>());

expr1.SetFomular("ISNUMBER(\"23\")");
Assert.AreEqual(true, expr1.Eval<bool>());

expr1.SetFomular("ISNUMBER(\".23\")");
Assert.AreEqual(true, expr1.Eval<bool>());

expr1.SetFomular("ISNUMBER(\"23.\")");
Assert.AreEqual(true, expr1.Eval<bool>());

expr1.SetFomular("ISNUMBER(\"-23\")");
Assert.AreEqual(true, expr1.Eval<bool>());

expr1.SetFomular("ISNUMBER(\"-.23\")");
Assert.AreEqual(true, expr1.Eval<bool>());

expr1.SetFomular("ISNUMBER(\"-23.\")");
Assert.AreEqual(true, expr1.Eval<bool>());

expr1.SetFomular("ISNUMBER(\".23.\")");
Assert.AreEqual(false, expr1.Eval<bool>());

expr1.SetFomular("ISNUMBER(\"0.2a3\")");
Assert.AreEqual(false, expr1.Eval<bool>());

Expand Down Expand Up @@ -1150,6 +1167,28 @@ public void Text_Function_Isblank_Test()
Assert.AreEqual(false, expr1.Eval<bool>());
}

[TestMethod]
public void Text_Function_Value_Test()
{
Expression expr1 = new Expression("VALUE(\"123\")");
Assert.AreEqual(123, expr1.Eval<int>());

expr1.SetFomular("VALUE(\"123.123\")");
Assert.AreEqual(123.123m, expr1.Eval<decimal>());

expr1.SetFomular("VALUE(\".123\")");
Assert.AreEqual(0.123m, expr1.Eval<decimal>());

expr1.SetFomular("VALUE(\"-.123\")");
Assert.AreEqual(-0.123m, expr1.Eval<decimal>());

expr1.SetFomular("VALUE(\"123.\")");
Assert.AreEqual(123, expr1.Eval<int>());

expr1.SetFomular("VALUE(\".123.\")");
Assert.ThrowsException<Exception>(() => expr1.Eval<int>());
}

[TestMethod]
public void Literals_Operation_Test()
{
Expand All @@ -1169,19 +1208,19 @@ public void Or_Operator_Test()
.Bind("a", 0)
.Bind("b", 0);
Assert.AreEqual(false, expr.Eval<bool>());

// false, true = true
var expr2 = new Expression("OR (a>0, b>0)")
.Bind("a", 0)
.Bind("b", 1);
Assert.AreEqual(true, expr2.Eval<bool>());

// true, false = true
var expr3 = new Expression("OR (a>0, b>0)")
.Bind("a", 1)
.Bind("b", 0);
Assert.AreEqual(true, expr3.Eval<bool>());

// true, true = true
var expr4 = new Expression("OR (a>0, b>0)")
.Bind("a", 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ THE SOFTWARE.
using org.matheval.Common;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace org.matheval.Functions
{
Expand Down Expand Up @@ -52,8 +51,7 @@ public List<FunctionDef> GetInfo()
/// <returns>Value</returns>
public Object Execute(Dictionary<string, Object> args, ExpressionContext dc)
{
Regex rg = new Regex("^-?\\d*(\\.\\d+)+$");
return rg.IsMatch(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture));
return decimal.TryParse(Afe_Common.ToString(args[Afe_Common.Const_Key_One], dc.WorkingCulture), out _);
}
}
}