How do I format a number to 2 decimal places, but only if there are already decimals? June 4, 2023 by Tarik This should do the job: var formattedNumber = (x * 1.5).toFixed(2).replace(/[.,]00$/, "");