Skip to content

Commit 71f4485

Browse files
committed
Merge branch 'scientific-number' into develop
* scientific-number: Add fix to changelog Handle scientific notation in to_number()
2 parents 7d86c36 + 38b6c8f commit 71f4485

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Next Release (TBD)
44
* Fix issue where long types in py2 and ``Decimal`` types
55
were not being evaluated as numbers
66
(`issue 125 <https://github.com/jmespath/jmespath.py/issues/125>`__)
7+
* Handle numbers in scientific notation in ``to_number()`` function
8+
(`issue 120 <https://github.com/jmespath/jmespath.py/issues/120>`__)
79

810

911
0.9.2

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)