Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Javascript (https://www.askmehelpdesk.com/forumdisplay.php?f=452)
-   -   How to optimise output for a thousandth breakdown (for currency)? (https://www.askmehelpdesk.com/showthread.php?t=367728)

  • Jun 22, 2009, 06:20 AM
    banjax
    How to optimise output for a thousandth breakdown (for currency)?
    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
  • Jun 22, 2009, 07:34 AM
    Perito
    There is a NumberFormat.getInstance(Locale.______); function that will format numbers. You'll probably have to assign the value to a string and then print the string rather than trying to incorporate it all onto one line.

    Formatting Numbers in Java

  • All times are GMT -7. The time now is 07:49 PM.