Skip to content

Commit

Permalink
fix: correctly parse dollars and cents from string representation
Browse files Browse the repository at this point in the history
  • Loading branch information
tdgrunenwald committed Dec 6, 2024
1 parent f513d33 commit ca7cace
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion features/steps/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ def _(context):
context.currency = Currency(value=USD.toValue(context.repr), format=USD)


@then("the resulting currency object has value {value}")
@then("the resulting currency object has value {value:d}")
def _(context, value: int):
assert context.currency.value == value, f"{context.currency.value} != {value}"
15 changes: 10 additions & 5 deletions src/ddd_sample/domain/currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ class USD(CurrencyFormatMixin):
def toValue(cls, repr: str) -> int:
repr = repr.strip()
repr = repr.replace(",", "")
match = re.search(r"^\$(\d*)\.?(\d?\d?)$", repr)
print(match.groups())
# dollars = int(match.group(1))
# cents = int(match.group(2))
groups = re.search(r"^\$(\d*)\.?(\d?\d?)$", repr).groups()

return 0#dollars * 100 + cents
dollars_str = groups[0]
dollars = int(dollars_str) if dollars_str else 0

cents_str = groups[1]
if cents_str and 1 == len(cents_str):
cents_str += '0'
cents = int(cents_str) if cents_str else 0

return dollars * 100 + cents

@abstractclassmethod
def toString(cls, value: int) -> str:
Expand Down

0 comments on commit ca7cace

Please sign in to comment.