Skip to content

Commit

Permalink
Fix for get_numeric_if_possible(-xxx)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarans committed Oct 21, 2020
1 parent 6b05b71 commit 4b8a01b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/hdx/utilities/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ def get_int_value(val, denominator):
if isinstance(value, str):
val = value.strip()
if val != '' and only_allowed_in_str(val, allowed_numeric):
try:
minusindex = val.index('-')
if minusindex != 0:
return val
except ValueError:
pass
try:
commaindex = val.index(',')
except ValueError:
Expand Down
3 changes: 3 additions & 0 deletions tests/hdx/utilities/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,17 @@ def test_get_numeric_if_possible(self):
assert get_numeric_if_possible('') == ''
assert get_numeric_if_possible('hello') == 'hello'
assert get_numeric_if_possible('123') == 123
assert get_numeric_if_possible('-123') == -123
assert get_numeric_if_possible('123.45') == 123.45
assert get_numeric_if_possible('-123.45') == -123.45
assert get_numeric_if_possible('123,123,123.45') == 123123123.45
assert get_numeric_if_possible('123.123.123,45') == 123123123.45
assert get_numeric_if_possible('123,123,123') == 123123123
assert get_numeric_if_possible('123.123.123') == 123123123
assert get_numeric_if_possible('12.3%') == approx(0.123)
assert get_numeric_if_possible('10%') == 0.1
assert get_numeric_if_possible('-10%') == -0.1
assert get_numeric_if_possible('10-') == '10-'
assert get_numeric_if_possible('123,123.45%') == 1231.2345
assert get_numeric_if_possible('-123,123.45%') == -1231.2345
assert get_numeric_if_possible('123.123,45%') == 1231.2345

0 comments on commit 4b8a01b

Please sign in to comment.