troubleshooting Question

Java Script Format Currency

Avatar of cuconsortium
cuconsortiumFlag for United States of America asked on
Scripting LanguagesJavaScriptJScript
9 Comments1 Solution424 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
leakim971
Multitechnician
Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros