?
Solved

Format currency in javascript

Posted on 2008-02-07
4
Medium Priority
?
470 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:michael1174
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
contactkarthi earned 2000 total points
ID: 20843999
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
 
LVL 2

Author Comment

by:michael1174
ID: 20844281
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
 
LVL 2

Author Closing Comment

by:michael1174
ID: 31428948
Thanks, ingnore my comment below.. this works great!
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20844426
it works great

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

maybe you should have onBlur

B CAPS
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question