1

How would I display JavaScript output numbers in dollar format? I.E. $20.00 then $2,000.00 when the number gets larger. Ok so sample code.

if(this.getField("Account Name RequiredRow1").value !="") {
    event.value = 20;
}
else{
    event.value = "";
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Michael Downey
  • 687
  • 3
  • 13
  • 42

2 Answers2

3
function formatCurrency(num) {
    num = isNaN(num) || num === '' || num === null ? 0.00 : num;
    return '$' + parseFloat(num).toFixed(2);
}

This is the easiest way

Senad Meškin
  • 13,597
  • 4
  • 37
  • 55
2

Here's the function that I use.. Basically the same as @Senad's except it adds commas:

function(val) {
    var valString = val.toFixed(2).toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
    return "$" + valString;
}
Bobby D
  • 2,129
  • 14
  • 21