import { formatAmount } from "./format"; /** * Parses what a person types into an amount field ("1,25,000", "1250.5", "₹ 99.99") into integer paise * without going through floating point. Returns null for anything that is not a plain amount with at * most two decimals; an empty string is 0. */ export function parseRupeesToPaise(text: string): number | null { const cleaned = text.replace(/[₹,\s]/g, ""); if (cleaned === "") return 0; const m = /^(\d+)(?:\.(\d{0,2}))?$/.exec(cleaned); if (!m) return null; const whole = Number(m[1]); const paise = whole * 100 + Number((m[2] ?? "").padEnd(2, "0")); return Number.isSafeInteger(paise) ? paise : null; } /** 123450 -> "1234.50", for filling an amount field. */ export function paiseToInput(paise: number): string { const p = Math.max(0, Math.round(paise)); return `${Math.floor(p / 100)}.${String(p % 100).padStart(2, "0")}`; } /** 123450 -> "₹1,234.50" */ export function formatPaise(paise: number): string { return `₹${formatAmount(paise / 100)}`; }