How to round numbers in After Effects

10th December 2016

Software(s): Adobe After Effects | Theme(s): Motion Design

You probably already looked for it on internet… You probably crushed your head into the table trying to round numbers in your composition on After Effects! Here is the solution: math.round();


//The function
Math.round('what you need to round')

//Returns 20
Math.round(20.49)

//Returns 21
Math.round(20.5)

//Returns -20
Math.round(-20.5)

We often use the function math.round() to round a dynamic number, for example, a number from a “Slider Control” value. If you link your Source Text to you Slider value (Alt+Click on the clock icon then link it to the Slider value)¬† you’ll see a weird number.

how_to_round_numbers_in_ae_01

how_to_round_numbers_in_ae_02

It will return you a value in terms of the Slider position. But what we would like to do is to round this number to make the animation smoother. In order to make this you need to wrap the expression returned by After Effects with the Math.round() function:


Math.round(effect("Slider Control")("Slider"))

And Voil√†! Here we are, we just got a beautiful round number… But as we are never satisfated, now we want to round up this number to two decimal places. In this kind of situation, remember, there is always a tip, a way to “cheat”, simply multiply the number by 100 and divide the all thing (the wrap) by 100 as well.


//Our example
Math.round(effect("Slider Control")("Slider")*100)/100

//Returns 12345.6
Math.round(123456*10)/10

//Returns 1234.56
Math.round(123456*100)/100

//Returns 123.456
Math.round(123456*1000)/1000

//etc etc

Update – 01/12/2017

The last way to round number was the “Tricky” way. Because when you wanna round numbers but you still wanna keep useless zeros after the comma it will cut them out!
Here is the proper way to do if you want to keep the full decimals. You can customise this expression, if you want more or less numbers before and after the comma by modifying the numDec and numDigit vars.


val = effect("Slider Control")("Slider");
numDec = 2; // digits to right of decimal
numDigit = 1; // digits to left of decimal
if (val < 0) sign = "-" else sign = ""; s = Math.abs(val).toFixed(numDec); while (s.length < numDigit + numDec + 1) s = "0" + s; sign + s

About the author

Bastien Delmare - Freelance Digital Designer
http://bastiendelmare.com

I'm a digital mercenary. During the last years I've been walking around the digital area, still learning everyday, this domain is an infinite source of inspiration who makes my days. Today as a freelancer I'm trying to take every project as a challenge because new knowledges, new technologies or innovative ideas are often required!