Skip to content

Commit a89505b

Browse files
committed
Handle scientific notation in to_number()
Instead just try to parse the number as an int, and if that fails try float() before returning None. Fixes #120.
1 parent 7d86c36 commit a89505b

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

jmespath/functions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ def _func_to_number(self, arg):
202202
return arg
203203
else:
204204
try:
205-
if '.' in arg:
206-
return float(arg)
207-
else:
208-
return int(arg)
205+
return int(arg)
209206
except ValueError:
210-
return None
207+
try:
208+
return float(arg)
209+
except ValueError:
210+
return None
211211

212212
@signature({'types': ['array', 'string']}, {'types': []})
213213
def _func_contains(self, subject, search):

tests/compliance/functions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,10 @@
495495
"expression": "to_number('1.0')",
496496
"result": 1.0
497497
},
498+
{
499+
"expression": "to_number('1e21')",
500+
"result": 1e21
501+
},
498502
{
499503
"expression": "to_number('1.1')",
500504
"result": 1.1

0 commit comments

Comments
 (0)