It took me a considerable amount of time to respond because I had to develop my own currency formatting function.
To see a demonstration, visit: http://jsfiddle.net/dm6LL/
The simple task of updating every second can be easily accomplished using JavaScript's command setInterval
.
setInterval(function(){
current += .158;
update();
},1000);
The update()
function in the code above is a basic updater that refers to an object with the id amount
to display the formatted current amount in a div on the page.
function update() {
amount.innerText = formatMoney(current);
}
In the update()
function, the variables amount and current are predefined:
var amount = document.getElementById('amount');
var current = 138276343;
Finally, the formatMoney()
function converts a number into a currency string.
function formatMoney(amount) {
var dollars = Math.floor(amount).toString().split('');
var cents = (Math.round((amount%1)*100)/100).toString().split('.')[1];
if(typeof cents == 'undefined'){
cents = '00';
}else if(cents.length == 1){
cents = cents + '0';
}
var str = '';
for(i=dollars.length-1; i>=0; i--){
str += dollars.splice(0,1);
if(i%3 == 0 && i != 0) str += ',';
}
return '$' + str + '.' + cents;
}