 ## Bastien Delmare

### Digital Art Direction & 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.  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
``````