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

try_parse_with_delim assumes positive amount #20

Open
rkost opened this issue Dec 9, 2022 · 0 comments · May be fixed by #21
Open

try_parse_with_delim assumes positive amount #20

rkost opened this issue Dec 9, 2022 · 0 comments · May be fixed by #21

Comments

@rkost
Copy link

rkost commented Dec 9, 2022

I just noticed that try_parse_with_delim assumes that a positive amount is given and will return incorrect results for negative inputs.

def try_parse_with_delim(s, d):
if d in s:
ps = s.split(d)
if len(ps) == 2:
if len(ps[1]) == 2:
return int(ps[0]) * 100 + int(ps[1]) * 1
elif len(ps[1]) == 1:
return int(ps[0]) * 100 + int(ps[1]) * 10
elif len(ps[1]) == 0:
return int(ps[0]) * 100
raise ValueError

The cent amount is added to the parsed euro amount, while it must be subtracted for negative inputs.

Example:

>>> try_parse_with_delim("-2.5", '.')
-150
@rkost rkost linked a pull request Dec 9, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant