Files

15 lines
476 B
JavaScript

export const parseMoneyToCents = (amount) => {
const amountParts = String(amount || "").match(/^(\d+)(?:\.(\d{1,2}))?$/);
if (!amountParts) return null;
return (
BigInt(amountParts[1]) * 100n +
BigInt((amountParts[2] || "").padEnd(2, "0"))
);
};
export const formatSignedMoney = (amount) => {
const normalizedAmount = String(amount);
const sign = normalizedAmount.startsWith("-") ? "-" : "+";
return `${sign}¥${normalizedAmount.replace(/^-/, "")}`;
};