Avatar of cuconsortium
cuconsortium
Flag for United States of America

asked on 

Java Script Format Currency

Dear all,

  I'm working with the attached java script function to mask a input text field to "$0.50 or $1,000.50 "

  When I run the page, I don't get error message and my input text field does not change to the above currency format either.

   I wonder what's going on with script.  I'm calling the function as follow:

<body>
<form id="form1" name="form1" method="post" action="">

    <input name="money" type="text" id="money" OnBlur="toCurrency(this)"/>

  </form>
</body>


Thank you!!
function toCurrency(which) 
{  

var num = which.value;

num = num.toString().replace(/\$|\,/g, '');  if (isNaN(num)) num = "0";  sign = (num == (num = Math.abs(num)));  num = Math.floor(num * 100 + 0.50000000001);  cents = num % 100;  num = Math.floor(num / 100).toString();  if (cents < 10) cents = '0' + cents;  for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3))  }  return (((sign) ? '' : '-') + '$' + num + '.' + cents)

}

Open in new window

Scripting LanguagesJavaScriptJScript

Avatar of undefined
Last Comment
cuconsortium

8/22/2022 - Mon