Link to home
Start Free TrialLog in
Avatar of cuconsortium
cuconsortiumFlag 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

Avatar of leakim971
leakim971
Flag of Guadeloupe image

after formating modify the value of the object : which.value = num;
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)

which.value = num;

}

Open in new window

Avatar of cuconsortium

ASKER

Hi leakim971,

  i added    which.value = num;    at the end of the function.  it still doesn't format the input value as currency...


 

eh I did not notice you return the formatted number at the end.
You can remove which.value = num; (line 8)

and update your input like this :
<input name="money" type="text" id="money" OnBlur="this.value = toCurrency(this)"/>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
great~~ it works!  But, now I got a bigger problem... I have several of these money fields, and have the other function written for Total Amount field, which will sum up all the money fields.  Is there any way to convert  the Masked data in money fields back to number, so then, the existing TotalAmount() function will work?

Thank you!
it look like a new question...

to back to a decimal number :

function toDecimal(which) { // $ab.xy
     which.value = which.replace(/\D/g, "") / 100;
}

Open in new window


or

function toDecimal(which) { // $ab.xy
     return which.replace(/\D/g, "") / 100;
}

Open in new window


When youv'e time check this : http://www.w3schools.com/jsref/jsref_tofixed.asp
ok. I'll open a new question.