37 lines
1.4 KiB
Python
37 lines
1.4 KiB
Python
"""Geldbeträge: ausschließlich Decimal, kaufmännisch gerundet, deutsches Format.
|
||
|
||
Rundung und Format kommen aus `butler/rechnen.py` (auch MCP-Server `rechnen`). `eur` bleibt hier: Es liest die
|
||
Schreibweise der Fallakten (Punkt = Tausender) und ist damit nachsichtiger als `rechnen.zahl`."""
|
||
from decimal import Decimal
|
||
from fractions import Fraction
|
||
|
||
from butler.rechnen import CENT, fmt, r2 # noqa: F401
|
||
|
||
|
||
def eur(text: str | int | Decimal) -> Decimal:
|
||
"""'1.139,27' / '1139,27' / '-41,04' -> Decimal. Floats werden abgelehnt."""
|
||
if isinstance(text, float):
|
||
raise TypeError("Floats sind für Geldbeträge verboten")
|
||
if isinstance(text, (int, Decimal)):
|
||
return Decimal(text)
|
||
s = text.replace("€", "").replace("EUR", "").replace(" ", "").strip()
|
||
s = s.replace(".", "").replace(",", ".")
|
||
return Decimal(s)
|
||
|
||
|
||
def bruch(text: str) -> Fraction:
|
||
"""'1/2' -> Fraction(1, 2); '95/188' ebenso; '1' -> 1."""
|
||
return Fraction(text.replace(" ", ""))
|
||
|
||
|
||
def mal(betrag: Decimal, faktor: Fraction | Decimal | int) -> Decimal:
|
||
"""Exakte Multiplikation, Rundung erst am Ende."""
|
||
if isinstance(faktor, Fraction):
|
||
return r2(betrag * faktor.numerator / Decimal(faktor.denominator))
|
||
return r2(betrag * Decimal(faktor))
|
||
|
||
|
||
def fmt_plain(value: Decimal) -> str:
|
||
"""Wie fmt, aber ohne Währungszeichen – für den Abgleich mit Belegtext."""
|
||
return fmt(value)[:-2]
|