Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Thanked 0 Times in 0 Posts

    Question How do I fix this?

    Can some one please help me with my java script?
    My java script consistently counts up, the problem is the cents character length is way too long (need 2 characters max). Another problem is, I don't know what code additionally I need to put commas in the correct position for determining the proper amount. Example: 12345.67 vs 12,345.67. If some one can just take a look at the code, modify it and re-post the full code since I have no idea what to do, I would deeply appreciate it.


  2. #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,947 Times in 4,908 Posts
    Next time, just post the code here instead of in some other place.

    Your code is both overkill and underkill.

    <style type="text/css">
    #counter {
        margin:0px 0px 0px 0px;
        float: left;
    #ds {
        margin:0px 0px 0px 7px;
        float: left;
        font-weight: bold;
    <p style="float:left;">Money Saved: </p>
    <p id="ds">$</p><div id="counter"></div>
    <script type="text/javascript">
    var START_DATE = new Date(2012,0,1,12,0,0); // january 1, 2012 12:00:00
    var INTERVAL = 1000; // savings per second
    var INCREMENT = 0.005; // money saved per second
    var START_VALUE = -50000; // configures proper savings calculation
    function tick( )
        var now = new Date();
        var count = parseInt((now.getTime() - START_DATE.getTime())/msInterval) 
                  * INCREMENT + START_VALUE;
        var pretty = count.toFixed(2);
        if ( pretty.length > 6 ) 
            pretty = pretty.substring(0,pretty.length-6) + "," + pretty.substring(pretty.length-6);
        document.getElementById('counter').innerHTML = pretty;
    setInterval( tick, INTERVAL );
    Be yourself. No one else is as qualified.

  3. #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,699 Times in 2,675 Posts
    Small error - should be

    var count = parseInt((now.getTime() - START_DATE.getTime())/INTERVAL) * INCREMENT + START_VALUE;

    Old Pedant's code will only insert commas up to $999,999.99. If you need bigger than that:-

    <script type="text/javascript">
    // works with negative numbers also
    function formatNumber(x) {
    return x.toFixed(2).split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
    var num = formatNumber(1234567.8897);
    num = "$" + num;
    alert (num);
    or add to Old Pedant's code to extend to 999999999.99

    if ( pretty.length > 10 )  {
    pretty = pretty.substring(0,pretty.length-10) + "," + pretty.substring(pretty.length-10);
    Last edited by Philip M; 06-26-2012 at 08:21 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


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