Format currency in javascript

Experts,

I have a formatCurrency function that I need to enhance.  I need it to be able to do the following:

If a user enters a H or h after a number, then multiply that number so it is formatted in the hundreds
If a user enters a T or t after a number, then multiply that number so it is formatted in the thousands
If a user enters a M or m after a number, then multiply that number so it is formatted in the milions

for example,

5m = 5,000,000
3T = 3,000

My function right now strips out characters.  I need it to not strip out those characters mentioned above and format it.


function formatCurrency(fld, curPrefix) {
	//Get rid of any thing other than numbers and decimal places.
 
	if(fld.value == ""){
	}
	else
	{
		num = fld.value;
		num = num.toString().replace(/[^0-9.]/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));
		}
 
 
		// Comment next line you do not want 2 decimal places.
		//fld.value =  ( ( (sign)?'':'-') + curPrefix + num + '.' + cents);
		//return (((sign)?'':'-') + curPrefix + num + '.' + cents);
 
		// UnComment next line you do not want 2 decimal places.
		fld.value =  (((sign)?'':'-') + curPrefix + num);
		return (((sign)?'':'-') + curPrefix + num);
	}
 
}

Open in new window

LVL 2
michael1174Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

contactkarthiCommented:
try this



<html>
<head> 
<script type="text/javascript" language="JavaScript"> 
function validate_form() {
formatCurrency(document.myForm.myText1, '$');
} 
function formatCurrency(fld, curPrefix) {
      //Get rid of any thing other than numbers and decimal places. 
      if(fld.value == ""){
      }
      else
      {
            var num = fld.value;
            if((num.toUpperCase()).charAt(num.length-1)=='T')
            num = num+',000';
            else if((num.toUpperCase()).charAt(num.length-1)=='M')
            num = num+',000,000'; 

            num = num.toString().replace(/[^0-9.]/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));
            } 

            // Comment next line you do not want 2 decimal places.
            //fld.value =  ( ( (sign)?'':'-') + curPrefix + num + '.' + cents);
            //return (((sign)?'':'-') + curPrefix + num + '.' + cents); 
            // UnComment next line you do not want 2 decimal places.
            fld.value =  (((sign)?'':'-') + curPrefix + num);
            return (((sign)?'':'-') + curPrefix + num);
      } 
}
</script> 

</head>
<body>
<form action="#" method="POST" name="myForm" >
<input type="text" name="myText1" >
<input type="button" value=convert onClick="validate_form()" > 
</form>
</body>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
michael1174Author Commented:
contactkarthi, I didn't get anything returned.

I am calling my function on the onblur event.  Is that the right event?

 onblur="formatCurrency(this,'$');"
0
michael1174Author Commented:
Thanks, ingnore my comment below.. this works great!
0
contactkarthiCommented:
it works great

<input type="text" name="myText1" onBlur="formatCurrency(this,'$')">

maybe you should have onBlur

B CAPS
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.