JSFiddle - React, Tailwind, and code Playground
HTML
<span>126913.36</span>
<span>0</span>
<span></span>
<span>0 m²</span>
CSS
span {display: block;
margin-bottom: 10px}
JavaScript
function commaSeparateNumber(val){
val = val.toString().replace(/,/g, ''); //remove existing commas first
var valSplit = val.split('.'); //then separate decimals
while (/(\d+)(\d{3})/.test(valSplit[0].toString())){
valSplit[0] = valSplit[0].toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
}
if(val == ""){
val = 0 + valSplit[0] + "." + 00+ 00;
}
else{
if(valSplit.length == 2){ //if there were decimals
val = valSplit[0] + "." + valSplit[1]; //add decimals back
}
if(valSplit.length == 1){
val = valSplit[0] + "." + 00+ 00;
}
}
return val;
}
$('span').each(function(){
var num = $(this).text();
var txt = '0.00 m²';
if(num == '0 m²'){
alert("gg")
$(this).text(txt);
}else{
var commaNum = commaSeparateNumber(num);
$(this).text(commaNum);
}
});