...

View Full Version : how to add commas to output? e.g. 1,000



mrgood
03-16-2009, 05:33 PM
i have this script here but im struggling to add commas to the output to seperate the figures. e.g. rather than 10000, I would like 10,000 here is the script:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
function Fires(id,fires){
var today=new Date();
var srt=new Date(today.getFullYear(),0,1,0,0,0,1);
var end=new Date(today.getFullYear(),11,31,24,0,0,-1);
document.getElementById(id).innerHTML=parseInt((today-srt)/(end-srt)*fires);
}

/*]]>*/
</script>
</head>

<body onload="Fires('fires',55000);" >
This year approximately <span id="fires" ></span> people have died in a fire.


</body>

</html>

Any help appreciated:thumbsup:

Philip M
03-16-2009, 05:51 PM
Try this:-



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>

<script type="text/javascript">
/*<![CDATA[*/

function Fires(id,fires) {
var today = new Date();
var srt = new Date(today.getFullYear(),0,1,0,0,0,1);
var end = new Date(today.getFullYear(),11,31,24,0,0,-1);
var nStr = parseInt((today-srt)/(end-srt)*fires).toString();
var rgx = /(\d+)(\d{3})/;
while (rgx.test(nStr)) {
nStr = nStr.replace(rgx, '$1,$2');
}
document.getElementById(id).innerHTML = nStr + " ";
}
/*]]>*/
</script>

</head>

<body onload="Fires('fires',55000);" >
This year approximately <span id="fires" ></span> people have died in a fire.

</body>

</html>



He thought he saw an Elephant,
That practised on a fife:
He looked again, and found it was
A letter from his wife.
"At length I realise," he said,
"The bitterness of Life!"
- Lewis Carroll

mrgood
03-16-2009, 07:08 PM
fantastic... this forum and the members are amazing!

Old Pedant
03-16-2009, 08:21 PM
Very cute trick, Philip.

I admit I would have brute forced it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum