diff --git a/web/src/components/Money.jsx b/web/src/components/Money.jsx index 5138311..14cdd50 100644 --- a/web/src/components/Money.jsx +++ b/web/src/components/Money.jsx @@ -1,7 +1,14 @@ import { formatMoney } from "../format"; // Zeigt einen Betrag in Kontowährung und – falls abweichend – zusätzlich den in -// die Hauptwährung umgerechneten Wert. `sign` färbt +/− (grün/rot). +// die Hauptwährung umgerechneten Wert. +// +// `sign` ist für Listen gedacht, in denen die Richtung aus der Buchungsart kommt +// (Ausgabe/Einnahme): dort wird der Betrag ohne Vorzeichen geführt und hier mit +// +/− und Farbe versehen. OHNE `sign` steht der Wert für sich selbst – etwa ein +// Kontostand – und wird mit seinem echten Vorzeichen ausgegeben. Früher lief er +// auch dann durch Math.abs(), wodurch ein überzogenes Konto oder eine +// Kreditkartenschuld fälschlich positiv aussah. export default function Money({ cents, currency = "EUR", @@ -10,7 +17,8 @@ export default function Money({ sign = null, // "pos" | "neg" | null showConverted = true, }) { - const cls = sign ? `amount ${sign}` : "amount"; + const negativ = !sign && Number(cents) < 0; + const cls = sign ? `amount ${sign}` : negativ ? "amount neg" : "amount"; const prefix = sign === "pos" ? "+" : sign === "neg" ? "−" : ""; const showBase = showConverted && baseCents != null && currency !== baseCurrency; @@ -18,10 +26,12 @@ export default function Money({ {prefix} - {formatMoney(Math.abs(cents), currency)} + {formatMoney(sign ? Math.abs(cents) : cents, currency)} {showBase && ( - ≈ {formatMoney(Math.abs(baseCents), baseCurrency)} + + ≈ {formatMoney(sign ? Math.abs(baseCents) : baseCents, baseCurrency)} + )} );