Files

20 lines
490 B
Python

from dataclasses import dataclass
from decimal import Decimal
@dataclass(frozen=True)
class Money:
amount: Decimal
def __post_init__(self):
if self.amount.as_tuple().exponent < -2:
raise ValueError(
f"Money amount cannot have more than 2 decimal places: {self.amount}"
)
def __add__(self, other):
return Money(self.amount + other.amount)
def __sub__(self, other):
return Money(self.amount - other.amount)