Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2003
    Location
    Devon, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Currency Format Help Please

    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
    Last edited by chris123; 01-19-2003 at 07:30 PM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    
    <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)...

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

    Code:
    
    <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/func...ts23/page1.asp

    Last edited by whammy; 01-19-2003 at 05:09 PM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #3
    New to the CF scene
    Join Date
    Jan 2003
    Location
    Devon, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Check out how I did it (using the same exact code) in this post:

    http://www.codingforums.com/showthre...threadid=13243

    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #5
    New to the CF scene
    Join Date
    Jan 2003
    Location
    Devon, UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Got it working now

    Thanks for you help


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •