Results 1 to 3 of 3

Thread: DD Countup Script: Thousands (Comma) Separator?

  1. #1
    Join Date
    Jul 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default DD Countup Script: Thousands (Comma) Separator?

    1) Script Title:
    Dynamic Countup Script

    2) Script URL (on DD):
    http://www.dynamicdrive.com/dynamici...countingup.htm

    3) Describe problem:
    This works great - only problem is formatting the thousands with a comma separator?

    Thanks in advance!

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    There are many premade functions online that format a number with comma separators, for example, this one. Using it, add the below function to the original script first:

    Code:
    function addCommas(nStr)
    {
    	nStr += '';
    	x = nStr.split('.');
    	x1 = x[0];
    	x2 = x.length > 1 ? '.' + x[1] : '';
    	var rgx = /(\d+)(\d{3})/;
    	while (rgx.test(x1)) {
    		x1 = x1.replace(rgx, '$1' + ',' + '$2');
    	}
    	return x1 + x2;
    }
    Then, inside the oncountup event handler, pass the "days" field into this function for processing first:

    Code:
    princewedding.oncountup=function(result){
    	//result is an object containing the current count up date/time, updated every second
    	//Available properties: result["days"], result["hours"], result["minutes"], and result["seconds"]
    	result["days"]=addCommas(result["days"])
    	var mycountainer=document.getElementById("cpcontainer")
    	mycountainer.innerHTML="Prince Charles and Camilla Parker have been married for: <br /><span class='dcountstyle'>"+result['days']+" <sup>days</sup> "+result['hours']+" <sup>hours</sup> "+result['minutes']+" <sup>minutes</sup> "+result['seconds']+" <sup>seconds</sup></span>"
    }

  3. #3
    Join Date
    Jul 2007
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you very much!

    the DD scripts are a godsend, I love it!

Bookmarks

Posting Permissions

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