JSFiddle - React, Tailwind, and code Playground
by techie bala
JavaScript
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
//return x1 + x2;
alert(x1 + x2);
}
function addCommas_RegEX(nStr)
{
var tmp = nStr.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
alert(tmp);
}
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g, '');
if (isNaN(num)) num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num * 100 + 0.50000000001);
cents = num % 100;
num = Math.floor(num / 100).toString();
if (cents < 10) cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
alert (((sign) ? '' : '-') + '$' + num + '.' + cents);
}
addCommas("1234567");
addCommas_RegEX("1234567");
formatCurrency("1234567");