Solved

# Format currency in javascript

Posted on 2008-02-07
Medium Priority
470 Views
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);
}

}
``````
0
Question by:michael1174
• 2
• 2

LVL 17

Accepted Solution

contactkarthi earned 2000 total points
ID: 20843999
try this

``````<html>
<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>

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

LVL 2

Author Comment

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

ID: 31428948
Thanks, ingnore my comment below.. this works great!
0

LVL 17

Expert Comment

ID: 20844426
it works great

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

maybe you should have onBlur

B CAPS
0

## Featured Post

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
Course of the Month4 days, 9 hours left to enroll