diginext-utils
Version:
README.md
11 lines (10 loc) • 400 B
JavaScript
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;