function numberFormat( num: number, decimals: number = 0, decimalSeparator: string = '.', thousandsSeparator: string = ',' ): string { if (isNaN(num)) { return '0'; } const fixedNum = num.toFixed(decimals); // Round to the specified decimal places const [integerPart, fractionalPart] = fixedNum.split('.'); // Split into integer and fractional parts // Add thousands separator to the integer part const formattedIntegerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator); // Combine integer and fractional parts return fractionalPart ? formattedIntegerPart + decimalSeparator + fractionalPart : formattedIntegerPart; } // Example Usage // console.log(numberFormat(1234567.89, 2)); // Output: "1,234,567.89" // console.log(numberFormat(1234567, 0)); // Output: "1,234,567" // console.log(numberFormat(1234.567, 3, ',', '.')); // Output: "1.234,567" export default numberFormat;