Java Script Format Currency

cuconsortium
cuconsortium used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
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

leakim971Multitechnician
Top Expert 2014

Commented:

Author

Commented:
Hi leakim971,

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


 

Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

leakim971Multitechnician
Top Expert 2014

Commented:
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

Multitechnician
Top Expert 2014
Commented:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
    window.onload = function() {
        var inputs = document.getElementsByTagName("input"); 
        for(var i=0;i<inputs.length;i++) {
	        inputs[i].value = toCurrency(inputs[i]);
        }
    }
    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);
    }
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
	<input name="money" type="text" id="money" OnBlur="this.value = toCurrency(this)"/>
</form>
</body>
</html>

Open in new window

Author

Commented:
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!
leakim971Multitechnician
Top Expert 2014

Commented:
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

Author

Commented:
ok. I'll open a new question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial