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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to create a thousandth breakdown in JavaScript.

    Hello there,

    I am having bother with fixing an output of some data calculations I have into thousandths. I require the output to place a comma where a thousandth is formed, so it's an easier read currency. I also have these fields set to 2 decimal places though, so I wonder if there is anyway to include this in the script?

    As you can see from this code below, I have used the .toFixed(-) code, therefore I require a method to break down the thousandths in the same way.

    Code:
    <script language="JavaScript">
    
    function showpay() {
     if ((document.calc.a.value == null || document.calc.a.value.length == 0) ||
         (document.calc.b.value == null || document.calc.b.length == 0) ||
         (document.calc.c.value == null || document.calc.c.value.length == 0) ||
         (document.calc.d.value == null || document.calc.d.value.length == 0) ||
         (document.calc.e.value == null || document.calc.e.value.length == 0))
    	  
     { alert("Please enter all values");
     }
     else
     {
     var aa = document.calc.a.value;
     var ab = document.calc.b.value;
     var ac = document.calc.c.value;
     var ad = document.calc.d.value;
     var ae = document.calc.e.value;
    
     var af = document.calc.f.value;
     var ag = document.calc.g.value;
     var ah = document.calc.h.value;
      
     var ai = document.calc.i.value;
     var aj = document.calc.j.value;
     var ak = document.calc.k.value;
    
     var al = document.calc.l.value;
     var am = document.calc.m.value;
     var an = document.calc.n.value;
     var ao = document.calc.o.value;
     
     var ap = document.calc.p.value;
     var aq = document.calc.q.value;
     var ar = document.calc.r.value;
     var as = document.calc.s.value;
     
     var at = document.calc.t.value;
     var au = document.calc.u.value;
     var av = document.calc.v.value;
     var aw = document.calc.w.value;
    
    // calculations
    
     document.calc.f.value = ab * ac;
     document.calc.g.value = ((ac * ad).toFixed( 2 ));
     document.calc.h.value = ((ae / ag).toFixed( 2 ));
     
     document.calc.i.value = ((ah / 4.3).toFixed( 2 ));
     document.calc.j.value = ((ai / 4.3).toFixed( 2 ));
     document.calc.k.value = ((ah / 52).toFixed( 2 ));
     
     document.calc.l.value = aa * 1;
     document.calc.m.value = ("$" +(aa * af).toFixed( 2 ));
     document.calc.n.value = ad * af;
     document.calc.o.value = ("$" +(aa * ag).toFixed( 2 ));
     
     document.calc.p.value = aa * 4.3;
     document.calc.q.value = ("$" +(af * ap).toFixed( 2 )); 
     document.calc.r.value = ad * ap;
     document.calc.s.value = ("$" +(ag * ap).toFixed( 2 ));
     
     document.calc.t.value = ap * 12; 
     document.calc.u.value = ("$" +(af * at).toFixed( 2 ));
     document.calc.v.value = ad * at;
     document.calc.w.value = ("$" +(ag * at).toFixed( 2 ));
    
     }
    }
    
    </script>
    Many thanks in advance.

    DS

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,318
    Thanks
    203
    Thanked 2,566 Times in 2,544 Posts
    Here you are.

    Code:
    <script type = "text/javascript">
    var num = 1234567890123.45678   //works with negative numbers as well 
    
    document.write('<span id="count"></span>');
    
    document.getElementById('count').innerHTML = addCommas(num);
    
    function addCommas(num) {
    nStr = num.toFixed(2);  // num is now a string
    nStr = nStr.replace(/[^0-9\.]/g,"");
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(nStr)) {
    nStr = nStr.replace(rgx, '$1,$2');
    }
    return nStr;
    }
    
    </script>
    You do not seem to have taken much notice of the advice/help you were given in http://www.codingforums.com/showthread.php?t=169639


    I have nothing but confidence in you. And very little of that.
    Groucho Marx (1890 - 1977)


  •  

    Tags for this Thread

    Posting Permissions

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