...

View Full Version : How to create a thousandth breakdown in JavaScript.



banjax
06-22-2009, 03:16 PM
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.


<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

A1ien51
06-22-2009, 03:54 PM
Plenty of examples out there:

http://www.google.com/search?q=javascript+thousand+separator
http://www.google.com/search?q=javascript+thousand+comma

Eric

Philip M
06-22-2009, 04:18 PM
Here you are.


<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)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum