Add commas and create percents
by chrislkeller
HTML
<div id="commasDiv"></div>
<br />
<div id="percentDiv"></div>
JavaScript
//begin main function
$(document).ready(function () {
dataCommas();
dataPercent();
});
//end main function
//begin function
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;
}
//end function
//add commas to numbers
function dataCommas() {
$('#commasDiv').html(
'<p>Wards Reporting: ' + addCommas(1000) + ' of ' + addCommas(2000) +
' (' + (1000/2000 * 100.0).toFixed(2) + '%)</p>');
}
//end function
//create percents from numbers
function dataPercent() {
$('#percentDiv').html(
'<p>Total Votes: 1234 (' + (1234/3000 * 100.0).toFixed(2) +
'%)</p>');
}
//end function