Amba-Agent-App/lib/utils/monetaryUtils.ts
2026-01-16 00:22:35 +03:00

40 lines
881 B
TypeScript

import { Big } from 'big.js';
export const parseDisplayToCents = (value: string): number => {
if (!value || value === "") return 0;
// Handle edge cases
if (value === "." || value === "0.") return 0;
try {
const bigValue = new Big(value);
// Multiply by 100 and round to get cents
return bigValue.times(100).round(0).toNumber();
} catch (error) {
return 0;
}
};
export const formatDisplayAmount = (value: string): string => {
if (!value || value === "") return "0.00";
// Handle edge cases
if (value === ".") return "0.00";
if (value === "0.") return "0.00";
if (value.endsWith(".")) {
return value + "00";
}
if (value.includes(".") && value.split(".")[1].length === 1) {
return value + "0";
}
try {
const bigValue = new Big(value);
return bigValue.toFixed(2);
} catch (error) {
return "0.00";
}
};