Today’s post is about toFixed & toPrecision Method in After Effects. So without any further ado let’s dive right in!
Alright, so we are in After Effects and for this example, I’m going to go inside of this UKRAMEDIA.COM text and I’m going to ALT+CLICK (OPT+CLICK on a Mac) on the stopwatch icon of my source text to create an expression.
Next, I’m going to define a variable inside of the white text box. I am going to say n, you’re going to be and then I’ll say time * 100.
Then I’m going to add a dollar sign plus the value n.
“$” = n
If I let go, you can see how I have the dollar sign plus the value.
There are a lot of numbers in here, so let’s format this number. Let’s talk about this. So how do I only keep the whole number with no decimals? That’s where toFixed comes in handy.
I am going to keep going and I am going to say .toFixed() and if I leave the toFixed() parentheses blank, it is going to give me the whole number.
“$” = n.toFixed()
That’s exactly what I am after here but what if let’s say I wanted to keep the whole number plus one decimal. How do I make that happen? Let me show you!
So inside of the toFixed() parentheses I’m going to say 1.
“$” = n.toFixed(1)
And as you do that, it is going to add one decimal.
Now, what if let’s say I want to have three decimals? Easy! Just replace 1 with 3.
So you get the idea of how easy this is. Now, what if let’s say I have a border around my numbers and I wanted to keep my value fixed at certain numbers.
I want to have a dollar sign plus 4 digits inside of the border. Right now it starts out with 4 digits but then it grows and because of that, it gets bigger and it kind of gets out of my fixed border. That’s a problem. How do I keep it restricted to only let’s say 4 digits the whole time so that way it stays inside of my fixed border? That is where the toPrecision() comes in handy.
So I am going to replace toFixed() with toPrecision() and inside of the toPrecision() parenthesis I am going to say 4 because we want to keep it to four digits.
“$” = n.toPrecision(4);
I hope you found this post useful. If you would like to learn more about Expression and how it can speed up your workflow in After Effects, check out my Learn After Effects Expressions Course.