Skip to content

Commit

Permalink
Merge pull request #368 from catull/fix-for-issue-367
Browse files Browse the repository at this point in the history
Accept trimmed decimal numbers; i.e. make ".5" / "-.5" valid numbers
  • Loading branch information
larsga authored Nov 28, 2024
2 parents b008c8e + 4c41ade commit c7bc579
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ private static JsonNode parseNumber(String number) {
if (number.length() == 0)
return null;

if (number.startsWith("-.")) {
number = "-0." + number.substring(2);
}

if (number.startsWith(".")) {
number = "0" + number;
}

int sign = 1;
int pos = 0;
if (number.charAt(0) == '-') {
Expand Down
5 changes: 0 additions & 5 deletions core/src/test/resources/function-error-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@
"query": "number(\"123.\")",
"input": "{}"
},
{
"error": ".345",
"query": "number(\".345\")",
"input": "{}"
},
{
"error": "1.1e",
"query": "number(\"1.1e\")",
Expand Down
10 changes: 10 additions & 0 deletions core/src/test/resources/function-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,16 @@
"input" : "{}",
"output" : "-0.513"
},
{
"query" : "number(\"-.513\")",
"input" : "{}",
"output" : "-0.513"
},
{
"query" : "number(\".513\")",
"input" : "{}",
"output" : "0.513"
},
{
"input" : "{}",
"output" : "2",
Expand Down
2 changes: 2 additions & 0 deletions functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ number(23) => 23
number("23") => 23
number("023") => 23
number(23.0) => 23.0
number(.23) => 0.23
number(-.23) => -0.23
number(null) => null
number("ab") => error
```
Expand Down

0 comments on commit c7bc579

Please sign in to comment.