In this post, I will show you 4 different ways to round numbers in After Effects.
First, I am going to ALT+Click on the Source Text stopwatch icon of the Ukramedia.com text layer.
Next, I will give it a variable t. I am going to say t, you are going to equal to time * 20. Then I will say t in the new line like so:
t = time*20;
t
As you can see from the screenshot above, right now we have number 12.6793450126793. How do we run this up to 13? Let me show you 4 options.
Option 1: toFixed()
The first option is the one we have gone over before. It is called toFixed.
In this example, I am going to add to the t variable below t = time*20;.
I am going to say t.toFixed() and when I do that, you can see that it did round it up to 13.
t = time*20;
t.toFixed()
The cool thing about toFixed() is that you can actually hold on to some decimals inside of the parenthesis. For example, we can type 2 inside toFixed(2) and it is going to give us 2 decimals like so:
If you type 4, then it is going to give you 4 decimals.
Option #2: Math.round()
The second method is probably the most common. It is called Math.round().
All you do is just type Math.round() and inside the parentheses, we are going to add t and that will round it up.
t = time*20;
Math.round(t)
Now the third and fourth methods are a bit more specific. let me show you what I am talking about.
I am going to go back to the beginning. I am going to type t in the second line so we can see our numbers again.
t = time*20;
t
In some cases, you want to round numbers down. For example, right now I have the number 12.6793460126793.
What if I wanted to round this number downwards? We cannot use toFixed() and Math.round() methods because those two will round everything up for us instead.
So how do I round downwards? In other words, I want the number to be 12. all the time until we change a number.
Option #3: Math.floor()
So to round this number downwards, we will use Math.floor() and inside the parenthesis, I am going to type t.
t = time*20;
Math.floor(t);
As you can see, right now it went down to 12 instead of 13. Now how do I do the opposite? How do I always round upwards? For example, I am going type t in the second line like so:
t = time*20;
t
This number right now normally would be rounded to 28, but how do I round it upwards? In other words, every time I
change this to a number, I want it to go upwards.
Option #4: Math.ceil()
We are going to use the ceiling method instead of the floor.
So I am going to say Math.ceil() and inside the parenthesis, I am going to put our variable t. Now watch what happens.
t = time*20;
Math.ceil(t)
And just like that, it rounded upwards, which is exactly what I wanted.
I hope you found this tip useful. I have created a course that goes deeper into the world of After Effects Expressions. Check it out!