• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

AE: add commas to number counting in expressions

New Here ,
Sep 24, 2017 Sep 24, 2017

Copy link to clipboard

Copied

I've found a few threads on how to have commas for numbers in expression, but I haven't been able to crack the code to add to the expression that I already have. It is as followed:

str = thisLayer.name;

if(str.length < 7){

pos = parseInt(str[6]);}

else{

pos = parseInt(str.substr(6,str.length));}

pos = pos-1;

duration = thisComp.layer("BarsController").effect("Animation duration labels")("Slider");

offset =  thisComp.layer("BarsController").effect("Animation labels start time offset")("Slider");

circle_offset = thisComp.layer("BarsController").effect("Animation bar scale offset")("Slider");

distance = thisComp.layer("BarsController").effect("Distance amount")("Slider");

label_Ypos = thisComp.layer("BarsController").effect("Labels distance")("Slider");

endAt = thisComp.layer("BarsController").effect("Animation labels end time offset")("Slider");

stay = thisComp.layer("BarsController").effect("Stay still until")("Slider")

if(time < stay+endAt){"$"+

Math.round(ease_in(time,1+pos*circle_offset+offset,1+pos*circle_offset+offset+duration,0,thisComp.layer(thisLayer.name.substr(5,  thisLayer.name.length)).effect("Value")("Slider")));}

else{"$"+

Math.round(ease_in(time,stay+endAt,stay+endAt+pos*circle_offset+offset+duration,thisComp.layer(thisLayer.name.substr(5,  thisLayer.name.length)).effect("Value")("Slider"),0))+"%";}

So trying to add commas to the numbers. Thanks!

Views

8.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 26, 2017 Sep 26, 2017

Now that I'm a little more awake here's the solution. Basically you have to define the calculated number as the s (short for string) value that Dan's punctuation expression expression is looking for. That needs to happen just after the { that starts the if calculation and just after the { that starts the else calculation.

You have manually added a leading $ to the text string and Dan's punctuation expression is going to see that as a column so it could add an extra comma. You have to remove the "

...

Votes

Translate

Translate
Community Expert ,
Sep 24, 2017 Sep 24, 2017

Copy link to clipboard

Copied

This was figured out a long time ago so I am giving you the link the the expression master: universal counter - Dan Ebberts's Expressioneering Design Guide

The guts of everything you need to add commas is right there.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

Thanks. I read it through and tried to add the comma code to different parts of my expression, but it keeps failing. Where should I drop it into my code?

Assuming this is what I need:

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);
  }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

The top part of Dan's universal counter declares the variables and starts the counter:

numDecimals = 2;

commas = true;

dollarSign = true;

beginCount = -1999;

endCount = 1999;

dur = 4;

t = time - inPoint;

s = linear (t, 0, dur, beginCount, endCount).toFixed(numDecimals);

The top five lines define variables. The next two lines generate the numbers. The variable "s" is the generated number that the rest of the expression looks at.

As you can probably guess you don't need decimals so you can change that variable to zero, you do need commas so you keep that true, and you don't need a dollar sign so you can change that variable to false.

You do not need the begin count and end count because you already have an expression that generates numbers. If you replace the t= and the s= lines with your expression you will be generating numbers again. Dan's expression is looking for the variable s to decide where to put the commas, add a dollar sign or add decimals. If you add an s = in front of your last line then the rest of Dan's expression should work just fine.

I hope that helps. It takes a while to understand how expressions work but if you try and understand each line then it will get easier to understand the logic.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

Thanks. I understand Dan's expression, but I'm trying to understand mine lol I tried this, but it failed:

numDecimals = 0;

commas = true;

dollarSign = false;

dur = 4;

t = time - inPoint;

s = thisLayer.name;

if(str.length < 7){

pos = parseInt(str[6]);}

else{

pos = parseInt(str.substr(6,str.length));}

pos = pos-1;

duration = thisComp.layer("BarsController").effect("Animation duration labels")("Slider");

offset =  thisComp.layer("BarsController").effect("Animation labels start time offset")("Slider");

circle_offset = thisComp.layer("BarsController").effect("Animation bar scale offset")("Slider");

distance = thisComp.layer("BarsController").effect("Distance amount")("Slider");

label_Ypos = thisComp.layer("BarsController").effect("Labels distance")("Slider");

endAt = thisComp.layer("BarsController").effect("Animation labels end time offset")("Slider");

stay = thisComp.layer("BarsController").effect("Stay still until")("Slider")

if(time < stay+endAt){"$"+

Math.round(ease_in(time,1+pos*circle_offset+offset,1+pos*circle_offset+offset+duration,0,thisComp.layer(thisLayer.name.substr(5,  thisLayer.name.length)).effect("Value")("Slider")));}

else{"$"+

Math.round(ease_in(time,stay+endAt,stay+endAt+pos*circle_offset+offset+duration,thisComp.layer(thisLayer.name.substr(5,  thisLayer.name.length)).effect("Value")("Slider"),0));}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

You have added the s = in the wrong place and I don't see the rest of Dan's expression.

You don't need dur, it has nothing to do with the way your expression generates numbers.

You don't need t = time, it has nothing to do with the way your expression generates numbers.

Dan's expression generates numbers and declares that number to be the variable s.

Replace the two layers t = and s = with your expression

Add s= to the last line of your expression because that is the line that generates the numbers

Then add the rest of Dan's expression which adds commas to the number defined by the variable s.

I'd do the copy and paste for you but it's hard to do on my phone...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

Sorry for me struggling with this haha

So I took the top half of the code then pasted my code where t= and s= were relacing it. Then added the rest of his code. Then I put s= at the end.....

numDecimals = 0;

commas = true;

dollarSign = false;

str = thisLayer.name;

if(str.length < 7){

pos = parseInt(str[6]);}

else{

pos = parseInt(str.substr(6,str.length));}

pos = pos-1;

duration = thisComp.layer("BarsController").effect("Animation duration labels")("Slider");

offset =  thisComp.layer("BarsController").effect("Animation labels start time offset")("Slider");

circle_offset = thisComp.layer("BarsController").effect("Animation bar scale offset")("Slider");

distance = thisComp.layer("BarsController").effect("Distance amount")("Slider");

label_Ypos = thisComp.layer("BarsController").effect("Labels distance")("Slider");

endAt = thisComp.layer("BarsController").effect("Animation labels end time offset")("Slider");

stay = thisComp.layer("BarsController").effect("Stay still until")("Slider")

if(time < stay+endAt){"$"+

Math.round(ease_in(time,1+pos*circle_offset+offset,1+pos*circle_offset+offset+duration,0,thisComp.layer(thisLayer.name.substr(5,  thisLayer.name.length)).effect("Value")("Slider")));}

else{"$"+

Math.round(ease_in(time,stay+endAt,stay+endAt+pos*circle_offset+offset+duration,thisComp.layer(thisLayer.name.substr(5,  thisLayer.name.length)).effect("Value")("Slider"),0));}

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;

}else{

  prefix + s;

}

s=

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

The last line of your Expression starts with Math.round. Change that to s=Math.round(ease . . .

You have to declare your variable before you use it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 25, 2017 Sep 25, 2017

Copy link to clipboard

Copied

I changed both, just the first one, then just the 2nd one, and then in front of the if with "s=" and it says there's an error on line 26 (same one that I've been getting).

If I remove all of Dan's script, the expression works (with out the commas), so I know I'm still screwing up on combining the code. Sorry!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

I apologize. I didn't carefully read you expression well enough. You have an if Statement buried in there so you need to put the s = inside the if statement. Try putting it right after the {.

The if would look like this:

if (time < stay+endAt) {s = "$"+

and the else

else { s = "$"+

And please put some spaces in your expressions. This one is almost impossible to decipher.

If I try your expression I get an error on Line 17 – "expecting a )" so I took a closer look. This is what you put on the forum:

str = thisLayer.name;

if(str.length < 7){

pos = parseInt(str[6]);}

else{

pos = parseInt(str.substr(6,str.length));}

pos = pos-1;

duration = thisComp.layer("BarsController").effect("Animation duration labels")("Slider");

offset =  thisComp.layer("BarsController").effect("Animation labels start time offset")("Slider");

circle_offset = thisComp.layer("BarsController").effect("Animation bar scale offset")("Slider");

distance = thisComp.layer("BarsController").effect("Distance amount")("Slider");

label_Ypos = thisComp.layer("BarsController").effect("Labels distance")("Slider");

endAt = thisComp.layer("BarsController").effect("Animation labels end time offset")("Slider");

stay = thisComp.layer("BarsController").effect("Stay still until")("Slider")

if(time < stay+endAt){"$"+

Math.round(ease_in(time,1+pos*circle_offset+offset,1+pos*circle_offset+offset+duration,0,t hisComp.layer(thisLayer.name.substr(5,  thisLayer.name.length)).effect("Value")("Slider")));}

else{"$"+

Math.round(ease_in(time,stay+endAt,stay+endAt+pos*circle_offset+offset+duration,thisComp.l ayer(thisLayer.name.substr(5,  thisLayer.name.length)).effect("Value")("Slider"),0));}

The line starts Math.round(ease_in(time,1+pos* and so on.

It looks like that is where multiple errors start. I don't know where you got ease_in( an array goes here)  The ease method is written  easeIn(t, tMin, tMax, value1, value2) or easeIn(t, value1, value2). ease_in is gibberish.

I can't figure out what you are trying to do with line 17. The array doesn't make much sense. Let me break it out, put some spaces in there and try and dissect it.

Math.round(easeIn(time, 1 + pos * circle_offset + offset, 1 + pos * circle_offset + offset + duration, 0, thisComp.layer(thisLayer.name.substr(5,  thisLayer.name.length)).effect("Value")("Slider")))

Fixing easeIn then starting the array (t, tMin, tMax, value1, value2) t = time, - that works.

tMin = gives you a number by taking the sum one plus the value variables derived from the sliders 1 + pos * circle_offset + offset

tMax = is the same value plus the duration value from a slider 1 + pos * circle_offset + offset + duration

value1 is simply set to zero 0

value2 is really confusing because of several things I'm going to try and figure them out.

thisComp.layer(thisLayer.name.substr(5,  thisLayer.name.length)).effect("Value")("Slider")

By the way, it would have been much easier to debug your expression if you had declared variables for t, tMax, tMin, value1 and value2 by putting them on separate lines. Something like tMin = 1 + pos * circle_offset + offset

Sorry, maybe because it is late and I've been up for more than 18 hours and I am tired, but I do not have any idea where you are going here. You have called for a value slider from a layer name derived from a substr???

I can't figure out what you are trying to do and cannot figure out what the purpose of the expression is. Fixing the easeIn method that is wrong twice and matching up the () I still can't get the expression to work. It looks like you are taking values of eight sliders and adding them to the string length of a layer name. If you need the commas to drive the size of a shape layer then your approach is awfully complicated.

Maybe if you shared your file through dropbox or something like that I could figure it out but I can't do it tonight. At least an explanation of what you are trying to achieve would help.

Sorry I couldn't be of more help. I spent about 30 minutes trying to figure out what you were trying to do.

One other problem I just found - the first part of your expression does not return a number:

str = thisLayer.name;

if(str.length < 7){

pos = parseInt(str[6]);}

else{

pos = parseInt(str.substr(6,str.length));}

pos = pos-1;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

V1 is the one I have been messing with and sequence V2 should have the original. Thanks!

PVG_MarketUpdate_InfoG_DB.aep

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

Now that I'm a little more awake here's the solution. Basically you have to define the calculated number as the s (short for string) value that Dan's punctuation expression expression is looking for. That needs to happen just after the { that starts the if calculation and just after the { that starts the else calculation.

You have manually added a leading $ to the text string and Dan's punctuation expression is going to see that as a column so it could add an extra comma. You have to remove the "$" but for reasons that I did not dig through you have to leave in the "".

I found the trouble by quickly going through the expression you wrote and giving it some punctuation and comments. I also corrected the ease_in method and basically used the default language from the Expression Language Menu > Interpolation helper and just added an e to the else arrays to keep them separated.

The corrected expression looks like this:

// punctuation setup

numDecimals = 0;

commas = true;

dollarSign = true;

// variables from sliders

duration = thisComp.layer("BarsController").effect("Animation duration labels")("Slider");

offset =  thisComp.layer("BarsController").effect("Animation labels start time offset")("Slider");

circle_offset = thisComp.layer("BarsController").effect("Animation bar scale offset")("Slider");

distance = thisComp.layer("BarsController").effect("Distance amount")("Slider");

label_Ypos = thisComp.layer("BarsController").effect("Labels distance")("Slider");

endAt = thisComp.layer("BarsController").effect("Animation labels end time offset")("Slider");

stay = thisComp.layer("BarsController").effect("Stay still until")("Slider")

// find layer and retrieve name

str = thisLayer.name;

if(str.length < 7){

  pos = parseInt(str[6]);}

else{

  pos = parseInt(str.substr(6,str.length));}

pos = pos-1;

t = time;

// calculate if values

tMin = 1+pos*circle_offset+offset;

tMax = 1+pos*circle_offset+offset+duration;

value1 = 0;

value2 = thisComp.layer(thisLayer.name.substr(5, thisLayer.name.length)).effect("Value")("Slider");

// calculate else values

etMin = stay+endAt;

etMax = stay+endAt+pos*circle_offset+offset+duration;

evalue1 = thisComp.layer(thisLayer.name.substr(5, thisLayer.name.length)).effect("Value")("Slider");

evalue2 = 0;

if(time < stay+endAt){

  s = ""+ Math.round(easeIn(t, tMin, tMax, value1, value2));}

else{

  s = ""+ Math.round(easeIn(t, etMin, etMax, evalue1 ,evalue2));}

// insert punctuation

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;

}else{

  prefix + s;

}

It is amazing how much easier it is to troubleshoot an expression if you give it a little air by using tabs and spaces. This is a much more complicated way of creating this data animation than I would have used. Index instead of layer name would be a good place to start. Expression controlled values for you bars would be another place. My approach would have been to use an index - argument and tie things together so one animated value would drive everything and all you had to do was duplicate the layer set to bring in more bars.

Anyway, copy and paste the expression and things should work. I hope you followed the logic.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

Thanks! Normally I do things from scratch so I don't run into issues like this, but I was on a time crunch and thought it would be easier piecing together the pieces from the client.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jan 17, 2023 Jan 17, 2023

Copy link to clipboard

Copied

LATEST

Just in case anyone stumbles across this outdated, and now totally incorrect, thread:

1. In File > Project Settings, under the Expressions tab, set "Expressions Engine" to "JavaScript".

2. Use this expression in your Source Text (assuming you have a Slider Control on that text layer, and want it to count up to a lot - hence the "*10" to multiply the value by 10). This also kills off the decimal places using the "toFixed" thing. If you want to keep the decimal places, just delete that second line, and change the "b" to an "a" in the last line. It's the "toLocaleString" bit that does the magic with the commas. You can Google that and change the formatting with some extra bits if you want - eg. 'en-US' to make it American-format:

a = effect("Slider Control")("Slider").value * 10;
b = a.toFixed(0);
Number(b).toLocaleString()

3. You're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines