UNPKG

diginext-utils

Version:
11 lines (10 loc) 400 B
import { toInt } from "../object"; /** * Format the input number with commas * @example 1000000 -> 1,000,000 */ export function formatNumber(num, prefix = "$") { const numRound = Math.round((toInt(num) + Number.EPSILON) * 100) / 100; return Number.isNaN(numRound) ? "0" : "" + numRound.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, prefix + "1,"); } export default formatNumber;