View Full Version : Currency Format Help Please

chris123

01-18-2003, 03:49 PM

I know you have been asked this thousands of time but i did a search and i dont seem able to relate it to me!

I am trying to format four currency feilds at once i have got it to calculate the feild right but now need to get it to add the zeros - Im way out of my depth here so if anybody could help...

If some body could explain to me what i need to do to format the text boxes at the bottom of my page i bwould much appreciate that

Cheers

Chris

EDIT: Link Removed

whammy

01-18-2003, 06:50 PM

I did some searching on the forums, and the only formatCurrency() function I found was one beetle uses (but didn't write) - I thought it was a bit lengthy, so I wrote this:

<script type="text/javascript">

<!--

function formatCurrency(num) {

num = !isNaN(num) ? Math.round(num * 100) / 100 : 0;

num.toString().indexOf(".") == -1 ? num += ".00" : void 0;

while(/\.\d{0,1}$/.test(num)) num += "0";

return num;

}

// -->

</script>

P.S. I noticed his function also added commas in the "thousands" or "millions" place, so I updated the above to do that as well if you wish (and this will do it no matter how many numbers are present)...

<script type="text/javascript">

<!--

function formatCurrency(num) {

var objRegExp = new RegExp('(-?\[0-9]+)([0-9]{3})');

num = num.toString().replace(/\,/g,"");

num = !isNaN(num) ? Math.round(num * 100) / 100 : 0;

num.toString().indexOf(".") == -1 ? num += ".00" : void 0;

while(/\.\d{0,1}$/.test(num)) num += "0";

while(objRegExp.test(num)) num = num.toString().replace(objRegExp,'$1,$2');

return num;

}

// -->

</script>

Here's the above script with notes added:

<script type="text/javascript">

<!--

function formatCurrency(num) {

/* Strip commas before processing */

num = num.toString().replace(/\,/g,"");

/* Round to two decimal places if longer */

num = !isNaN(num) ? Math.round(num * 100) / 100 : 0;

/* Add decimal and zeroes if none exist */

num.toString().indexOf(".") == -1 ? num += ".00" : void 0;

/* Add zeroes if only decimal exists or only one digit after decimal */

while(/\.\d{0,1}$/.test(num)) num += "0";

/* Add commas */

var objRegExp = new RegExp('(-?\[0-9]+)([0-9]{3})');

while(objRegExp.test(num)) num = num.toString().replace(objRegExp,'$1,$2');

/* Return formatted currency */

return num;

}

// -->

</script>

The comma addition regex may seem familiar; that's because I originally got it from Karen Gayda's script here (which, by the way, is one of the articles that introduced me to regular expressions):

http://www.siteexperts.com/tips/functions/ts23/page1.asp

:)

chris123

01-19-2003, 07:07 PM

cheers

very helpfull

It make so much more sence when it has some comments to go with the code!

just one last question but how do i relate that to each of the text boxes that have the numbers in them.

Is there a 'on change' function with html that can run the function when the value changes?

whammy

01-19-2003, 07:09 PM

Check out how I did it (using the same exact code) in this post:

http://www.codingforums.com/showthread.php?s=&threadid=13243

:)

chris123

01-19-2003, 08:24 PM

Got it working now

Thanks for you help :thumbsup:

Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.