@dimdevs/nusantara
Version:
A utility toolkit for formatting and validating data in the Indonesian context. From currency and dates to bank lists and national ID validation.
14 lines (13 loc) • 652 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatRupiah = formatRupiah;
function formatRupiah(value, options) {
const { withPrefix = true, prefix = 'Rp', decimal = false, decimalDigits = 2, separator = '.', } = options || {};
const num = typeof value === 'string' ? parseFloat(value) : value;
const parts = num
.toFixed(decimal ? decimalDigits : 0)
.split('.');
const formattedInt = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, separator);
const formatted = decimal ? `${formattedInt},${parts[1]}` : formattedInt;
return withPrefix ? `${prefix}${formatted}` : formatted;
}