SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist azizur_rahman's Avatar
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Java Script: FormatCurrency

    I use FormatCurrency in VB/VBS/VBA but can anyone tell how do i do the same in JavaScript.

    I need to format Item01SubTotal and Item01Price as Currency. the parseFloat() does not work: it shows up with more then 2 digits after the dicimal points.
    I want to round values to 2 digits after the dicimal;

    Tahnks in Advance.
    here is what I have so far:
    Code:
    function SelQuantity01_onchange() {
    	// Item01: Data
    	Item01Name = (document.getElementById("Item01Title").innerHTML);
    	Item01Quantity = document.fromMain.SelQuantity01.options[document.fromMain.SelQuantity01.selectedIndex].text;
    	Item01Price = document.fromMain.Item01Price.value;
    	Item01SubTotal = (parseInt(Item01Quantity) * parseFloat(Item01Price));
    	
    	infoStr = "Item" + "\t\t\t\t\t\t" + "Price" + "\t\t" + "Quantity"  + "\t\t" +  "Sub Total" + "\t\n";
    	infoStr += Item01Name + "\t" + Item01Price + "\t\t" + Item01Quantity  + "\t\t" +  Item01SubTotal + "\t\n";
    	alert(infoStr);
    }
    Azizur Rahman
    Web Application Developer

  2. #2
    SitePoint Addict ownersite's Avatar
    Join Date
    Jul 2001
    Posts
    222
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code works for me...
    Code:
    function formatCurrency(num) {
      num = num.toString().replace(/\$|\,/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)); 
      return (((sign)?'':'-') + num + '.' + cents);
    }
    Last edited by ownersite; May 22, 2002 at 11:05.
    F150online- 102,000 users strong and growing...
    Ownersite.com
    - tools to enhance vehicle ownership

  3. #3
    SitePoint Evangelist azizur_rahman's Avatar
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    thank you very much.

    It worked perfectly.
    Azizur Rahman
    Web Application Developer

  4. #4
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    azizur...

    This is the JAVA forum, not the javascript forum.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  5. #5
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the JAVA forum, not the javascript forum.
    Eh?


    M@rco

  6. #6
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this thread started out on the Java forum and was then moved here by a moderator. It's a shame vBulletin doesn't add some kind of indicator to a thread showing that it has been moved (an entry in the reply list just stating "This thread moved to FORUM-X from FORUM-Y at this point" would be fine).


Bookmarks

Posting Permissions

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