newmax-utils
Version:
Utils & Libs for Newmax Tech
18 lines (17 loc) • 578 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.thousandsSeparator = void 0;
function thousandsSeparator(value, d = 2) {
const str = value.toFixed(d).replace(/\s+/g, '');
if (str.includes('.')) {
const [wholePart, fractionalPart] = str.split('.');
return `${formatWholePart(wholePart)}.${fractionalPart}`;
}
else {
return formatWholePart(str);
}
}
exports.thousandsSeparator = thousandsSeparator;
function formatWholePart(wholePart) {
return wholePart.replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
}