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

    how to add commas to output? e.g. 1,000

    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:

    Code:
    <!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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Try this:-

    Code:
    <!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
    Last edited by Philip M; 03-16-2009 at 05:53 PM.

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts
    fantastic... this forum and the members are amazing!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Very cute trick, Philip.

    I admit I would have brute forced it.


  •  

    Posting Permissions

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