@prazzol/currency-formatter
Version:
Format currency in Nepali and international styles with custom prefix support.
35 lines (34 loc) • 1.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatCurrency = formatCurrency;
function formatCurrency(amount, options = {}) {
const { prefix = 'रू', showPrefix = true, format = 'nepali', locale = 'en-US', currency = 'NPR', } = options;
if (isNaN(amount))
return '';
const integerAmount = Math.floor(amount);
let formatted = '';
if (format === 'locale') {
const formatter = new Intl.NumberFormat(locale, {
style: 'currency',
currency,
maximumFractionDigits: 0,
});
formatted = formatter.format(integerAmount);
return formatted;
}
formatted =
format === 'international'
? formatInternational(integerAmount)
: formatNepali(integerAmount);
return showPrefix ? `${prefix} ${formatted}` : formatted;
}
function formatInternational(number) {
return number.toLocaleString('en-US');
}
function formatNepali(number) {
const str = number.toString();
const lastThree = str.slice(-3);
const rest = str.slice(0, -3);
const formattedRest = rest.replace(/\B(?=(\d{2})+(?!\d))/g, ',');
return rest ? `${formattedRest},${lastThree}` : lastThree;
}