### Format numbers to currency format

Use the three subfunctions below to reformat any amount in a currency format, complete with commas for thousands delimiter and two decimal points. (Example: 12,345.00)
JavaScript

FUNCTIONS

```//////////////////////////////////////////////////////////////////

function outputMoney(number) {
var cents = outputCents(number - 0);
if (cents=='.100') {
/* ---- needed for rouding to the next dollar ----- */
return outputDollars(Math.floor(number+1) + '') + '.00';
}else {
return outputDollars(Math.floor(number-0) + '') + cents;
}
}

//////////////////////////////////////////////////////////////////

function outputDollars(number) {
if (number.length <= 3)
return (number == '' ? '0' : number);
else {
var mod = number.length%3;
var output = (mod == 0 ? '' : (number.substring(0,mod)));
for (i=0 ; i < Math.floor(number.length/3) ; i++) {
if ((mod ==0) && (i ==0))
output+= number.substring(mod+3*i,mod+3*i+3);
else
output+= ',' + number.substring(mod+3*i,mod+3*i+3);
}
return (output);
}
}

//////////////////////////////////////////////////////////////////

function outputCents(amount) {
amount = Math.round( ( (amount) - Math.floor(amount) ) *100);
return (amount < 10 ? '.0' + amount : '.' + amount);
}

//////////////////////////////////////////////////////////////////```

CALLING FUNCTION SAMPLE

`newvalue = outputMoney(document.forms[0].SubTotal.value * exchangerate)`

Posted by fbrefere001 on Wednesday April 5, 2006