JSFiddle - React, Tailwind, and code Playground

by longvu

JavaScript

function formatMoney(number) {
	    number = parseFloat(number.toString().match(/^\d+\.?\d{0,2}/));
	    //Seperates the components of the number
	    var components = (Math.floor(number * 100) / 100).toString().split(".");
	    //Comma-fies the first part
	    components [0] = components [0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
	    //Combines the two sections
	    return components.join(".");
	}
alert(formatMoney(-11));