0

I have multiple text fields that, once filled, the final text field is updated automatically via javascript, my problem is that I'd like to format this last field as currency but I have no idea where to start.

Googled for days already. :(

Here's the JavaScript code:

function startCalc(){
  interval = setInterval("calc()",1);
}
function calc(){
  one = document.myform.numberof_golfers2.value;
  two = document.myform.numberof_golfers3.value;
  three = document.myform.selected_Rate.value;
  document.myform.Costs_Total.value = (one * 13040) + (two * 10280) + (three * 1);
}
function stopCalc(){
  clearInterval(interval);
}
Allan Kimmer Jensen
  • 4,333
  • 2
  • 31
  • 53
  • 1
    If by format as currency you mean add a currency symbol, you need something like `document.myform.Costs_Total.value = '$' + (one * 13040) + (two * 10280) + (three * 1);` – Asad Saeeduddin Oct 23 '12 at 11:01
  • Possible duplicate: http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript – jonvuri Oct 23 '12 at 11:07
  • @Asad, that actually won't work, because the first part of the expression coerces to a string, and the other two additions will be interpreted as string concatenations. – jonvuri Oct 23 '12 at 11:08
  • Correct, needs another bracket around all the integers. – Asad Saeeduddin Oct 23 '12 at 11:15
  • @Asad, sorry i'm new to these forums and don't spend alot of time on any forums really, my bad. So i've managed to get the currency symbol in... document.myform.Costs_Total.value = ('R') + ((one * 13040) + (two * 10280) + (three * 1)); but now need the comma to show up.... – user1768068 Oct 23 '12 at 11:43

1 Answers1

0

you could try pass the value through the below.

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

but please elaborate on your question like other poster suggested. What format do you want at the end?

topcat3
  • 2,561
  • 6
  • 33
  • 57