@lyxa.ai/types
Version:
Lyxa type definitions and validation schemas for both frontend and backend
28 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.roundSecondaryCurrency = exports.roundBaseCurrency = exports.roundCurrencySync = exports.SecondaryCurrencyRoundTo = exports.BaseCurrencyRoundTo = void 0;
exports.BaseCurrencyRoundTo = 0.01;
exports.SecondaryCurrencyRoundTo = 1000;
const roundCurrencySync = (amount, roundingFactor) => {
if (typeof amount !== 'number' ||
Number.isNaN(amount) ||
typeof roundingFactor !== 'number' ||
roundingFactor <= 0 ||
amount === 0) {
return 0;
}
const decimals = (roundingFactor.toString().split('.')[1] || '').length;
const safeAmount = Number(amount.toFixed(decimals + 2));
const rounded = Math.round(safeAmount / roundingFactor) * roundingFactor;
return Number(rounded.toFixed(decimals));
};
exports.roundCurrencySync = roundCurrencySync;
const roundBaseCurrency = (amount) => {
return (0, exports.roundCurrencySync)(amount, exports.BaseCurrencyRoundTo);
};
exports.roundBaseCurrency = roundBaseCurrency;
const roundSecondaryCurrency = (amount) => {
return (0, exports.roundCurrencySync)(amount, exports.SecondaryCurrencyRoundTo);
};
exports.roundSecondaryCurrency = roundSecondaryCurrency;
//# sourceMappingURL=currency.js.map