ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

counting numbers with commas

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Lisa Overton
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) + ''




Return to posts index

Dan Ebberts
Re: counting numbers with commas
on 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[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



Return to posts index

Declan Smith
Re: counting numbers with commas
on 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
http://www.madpanic.tv
After Effects CS6/ FCS3 / Canon XLH1 / Canon 7D / Reason / Cubase

"it's either binary or it's not"


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]