UNPKG

diginext-utils

Version:
15 lines (14 loc) 563 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatNumber = void 0; const object_1 = require("../object"); /** * Format the input number with commas * @example 1000000 -> 1,000,000 */ function formatNumber(num, prefix = "$") { const numRound = Math.round(((0, object_1.toInt)(num) + Number.EPSILON) * 100) / 100; return Number.isNaN(numRound) ? "0" : "" + numRound.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, prefix + "1,"); } exports.formatNumber = formatNumber; exports.default = formatNumber;