sanamare/src/lib/money.ts
Claude 6c784e8a30
Sanamare Shop: Grundgerüst, Storefront, Kasse, Zahlungen, Admin-Anfang
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SvuGvHpbAQ4syfuLuRvutL
2026-10-19 13:11:20 +00:00

28 lines
1 KiB
TypeScript

const fmt = new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" });
/** 12900 → "129,00 €" */
export function formatPrice(cents: number): string {
return fmt.format(cents / 100);
}
/** "129,00" / "129.5" / "129" → 12900; ungültig → null */
export function parsePrice(input: string | null | undefined): number | null {
if (input == null) return null;
const s = String(input).trim().replace(/\s|€/g, "");
if (!s) return null;
const normalized = s.includes(",") ? s.replace(/\./g, "").replace(",", ".") : s;
const n = Number(normalized);
if (!Number.isFinite(n) || n < 0) return null;
return Math.round(n * 100);
}
/** 12900 → "129,00" (für Eingabefelder) */
export function centsToInput(cents: number | null | undefined): string {
if (cents == null) return "";
return (cents / 100).toFixed(2).replace(".", ",");
}
/** Enthaltene MwSt. aus einem Bruttobetrag */
export function includedTax(gross: number, rate: number): number {
return Math.round(gross - gross / (1 + rate / 100));
}