JSFiddle - React, Tailwind, and code Playground
by Good Muyis
HTML
<h3>Format Currency</h3>
<!-- Format numbers to currency using a bit of JavaScript and CSS
Source: http://ozkary.blogspot.com/ Date: 04/27/2014 -->
<div id="stmt0">250</div>
<hr/>
<div id="stmt1">2500</div>
<hr/>
<div id="stmt2">25000</div>
<hr/>
<div id="stmt3">-250009</div>
<hr/>
<div id="stmt4"><span>Price:</span>2500000</div>
<hr/>
CSS
.enMoney::before {
content:"$";
}
.negMoney {
color:red;
}
div.negMoney::before {
content:'($';
}
div.negMoney::after {
content:')';
}
JavaScript
$('* div').each(function () {
var item = $(this).text();
var item = item.replace(/[^\d]/g, '');
var num = Number(item).toLocaleString('en');
if (Number(item) < 0) {
num = num.replace('-','');
$(this).addClass('negMoney');
}else{
$(this).addClass('enMoney');
}
$(this).text(num);
});