ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

counting numbers with commas

FAQ   •   VIEW ALL counting numbers with commas on Jan 28, 2016 at 3:23:02 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) + '' Re: counting numbers with commason Jan 28, 2016 at 6:00:38 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 == "-"){
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 Re: counting numbers with commason Jan 28, 2016 at 10:48:18 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