Skip to main content
JoshuaLisciarelli
Participant
November 8, 2019
Answered

How to limit the number of decimals a Slider shows

  • November 8, 2019
  • 4 replies
  • 1663 views

Hi all!

 

I'm currently working on a timer in AE — it will be used for SAT tests, so I have to make sure that it keeps perfect time. Here's the code I've used to create it:

 

slider=effect("Slider Control")("Slider");
sec=slider%60;
min=Math.floor(slider/60);

function addZero(X){
if(X<10)return"0"+X else return X;
}
addZero(min)+":"+addZero(sec)

 

While this function does keep perfect time, it displays all the decimals in between each whole second (00:00.96666666...). Is there a code I can insert that limits the displayed decimals to just the minutes and seconds, and doesn't round up the numbers? (I've found using    slider = Math.round(effect("Slider Control")("Slider"));    rounds the time up and displays the seconds before they should. EX: 00:02 shows up at 00:01:15 instead of 00:02:00)

 

Any and all help is greatly appreciated.
Thanks, have a great day!
— Joshua L

This topic has been closed for replies.
Correct answer Mike_Abbott

OK:

 

slider=effect("Slider Control")("Slider");
sec=Math.floor(slider%60);
min=Math.floor(slider/60);

function addZero(X){
if(X<10){
return"0"+X
}else{
return X
}
}
addZero(min)+":"+addZero(sec)

4 replies

Mike_Abbott
Mike_AbbottCorrect answer
Legend
November 8, 2019

OK:

 

slider=effect("Slider Control")("Slider");
sec=Math.floor(slider%60);
min=Math.floor(slider/60);

function addZero(X){
if(X<10){
return"0"+X
}else{
return X
}
}
addZero(min)+":"+addZero(sec)

Roland Kahlenberg
Legend
November 9, 2019

I strongly approve your OK. 🙂

Very Advanced After Effects Training | Adaptive &amp; Responsive Toolkits | Intelligent Design Assets (IDAs) | MoGraph Design System DEV
Mike_Abbott
Legend
November 9, 2019

: )

Roland Kahlenberg
Legend
November 8, 2019

That Expression will not work with AE's Javascript engine - it will throw up an error. Where did you get this script?

Very Advanced After Effects Training | Adaptive &amp; Responsive Toolkits | Intelligent Design Assets (IDAs) | MoGraph Design System DEV
Mike_Abbott
Legend
November 8, 2019

Just type : time

into the expression field of a text source property. Then hit the spacebar. It will output the 'current time' in seconds : )

 

PS: If you can hit 'correct answer' on my first reply that will mark this thread as answered.

JoshuaLisciarelli
Participant
November 8, 2019

How that sounds really helpful actually. Thanks for taking the time to explain!

 

Will do!

Mike_Abbott
Legend
November 8, 2019

Would replacing your second line with this help:

sec=Math.floor(slider%60);

 

But - wouldn't it be easier just to use the built in expression 'time' function to do this job? Or do you need the flexibility of this keyframeable approach?

JoshuaLisciarelli
Participant
November 8, 2019

That did the trick!! Thank you so much Mike!

 

I've actually never used that function before. Admittedly, I'm a bit new to After Effects. How does that work?

Or would you happen to have a link to a tutorial that explains how that works?

 

Thanks again, have a great day!
— Joshua L