Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions counting numbers with commas

  • counting numbers with commas

    Posted by Lisa Overton on January 28, 2016 at 3:23 pm

    I am using the following expression to create a number countdown but I need it to recognize or insert commas in eight digit numbers. Can anyone tell me how to alter it:

    P.S. there are also times that I’d like to add dollar signs or percent signs.

    startT = 0;
    endT = .5;
    beginVal = 12400000;
    endVal = 10700000;
    t = linear(time,startT,endT,beginVal,endVal);
    Math.floor(t) + ''

    Declan Smith replied 10 years, 7 months ago 3 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    January 28, 2016 at 6:00 pm

    This should do what you need:


    numDecimals = 0;
    commas = true;
    dollarSign = true;
    percentSign = false;
    startT = 0;
    endT = .5;
    beginCount = 12400000;
    endCount = 10700000;
    dur = 4;

    t = time - inPoint;
    s = linear (time, startT, endT, beginCount, endCount).toFixed(numDecimals);

    suffix = percentSign ? "%" : "";
    prefix = "";
    if (s[0] == "-"){
    prefix = "-";
    s = s.substr(1);
    }
    if(dollarSign) prefix += "$";

    if (commas){
    decimals = "";
    if (numDecimals > 0){
    decimals = s.substr(-(numDecimals + 1));
    s = s.substr(0,s.length - (numDecimals + 1));
    }
    outStr = s.substr(-s.length, (s.length-1)%3 +1);
    for (i = Math.floor((s.length-1)/3); i > 0; i--){
    outStr += "," + s.substr(-i*3,3);
    }
    prefix + outStr + decimals + suffix;
    }else{
    prefix + s + suffix;
    }

    Dan

  • Declan Smith

    January 28, 2016 at 10:48 pm

    Try adding this as your last line. The first part adds the “$” and the rest sorts out the commas. If you want to change how many numbers are needed before a comma, change the the number “7” below to what you need, say 3, which is standard.

    "$" + Math.round(t).toFixed(0).replace(/(\d)(?=(\d{7})+(?!\d))/g, "$1,");

    Declan Smith
    https://www.madpanic.tv
    After Effects CS6/ FCS3 / Canon XLH1 / Canon 7D / Reason / Cubase

    \”it\’s either binary or it\’s not\”

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy