Adding commas to animated numbers

This is something that I usually end up Googling every time I need to animate a countdown or a growing number in After Effects.

Basic Setup

  1. Add a Slider Control effect (see note) from the Expression Controls – adding it to your text layer makes it easier to pick-whip the expression later, but you can have it applied to any layer you choose. This is where you will keyframe your number value. I normally right-align the number so that as it grows it expands from the right, but adjust to taste.
  2. To apply an expression to the Source Text property of your text layer, Option+Click (Win: Alt+Click) the stopwatch. I set a variable to the Slider effect by typing in a variable name …
    var mySlider =

    … and then use the pick-whip to link to the Slider.

    var mySlider = effect("Slider Control")("Slider");
  3. Normally I am dealing with whole numbers, and I round down using the following expression:
    mySlider = Math.floor(mySlider)

This gets you far enough that you can animate the Slider effect and the source of your text layer will update. If you need to put a dollar sign at the beginning, your expression just needs one more line to join two strings together – your dollar sign and your dynamic number.

var myString = effect("Slider Control")("Slider");
myString = Math.floor(myString);
"$" + myString;

Note: The Slider Control will only provide a range of -1,000,000 to 1,000,000.

For an unlimited range, use a Point Control effect and animate your number in the x or y axis. You can pick-whip directly to either the x or y axis, or add a [0] or [1] to access the first (x) or second (y) value within the Point Control.

var mySlider = effect("Point Control")("Point")[0];

Comprehensive Setup with Commas

There is always more than one way to skin a cat, but I have been using the following expression to split a number into sections of three, and add commas in the breaks. I have collected the meat of it into a reusable function including comments throughout. You should be able to copy and paste this into your text layer Source Text expression to get started.

// Pick-whip number from Slider or Point Control and assign to mySlider
// For example: var mySlider = effect("Point Control")("Point")[0];

var mySlider = 123456789;

// Use the commas function defined below to format your number.
// Commas takes three properties - the number input, 
// the size of the groups of numbers and the number of decimal places.
// You can use this function and only give it the base number.

var numberstring = commas(mySlider);

// This is the final output of this expression.
// Use quotes around other text that you want to join with your numberstring variable.
// eg. '$' + numberstring + ' RAISED'

'$' + numberstring;

function commas(number, groupSize, decimals){

	// Default settings if only a number is provided.

	groupSize = (groupSize == undefined) ? 3 : groupSize;
	decimals = (decimals == undefined) ? 0 : decimals;
	var theComma = ",";

	// Round to given decimal places and convert number to string
	var strNumber = Number(number).toFixed(decimals).toString()

	// Store decimal places separately, with a bullet point if they exist or empty string if not.
	var arrNumber = strNumber.split('.')
	var decimalPlaces = (arrNumber.length > 1) ? '.' + arrNumber[1] : '';

	// Convert to array to split into characters
	var anum = arrNumber[0].split('');

	// New array to hold formatted number
	var fnum = new Array();

	var count = 0;

	// Process the characters in reverse, inserting a comma after each groupSize.
	for(var i = anum.length-1; i >= 0; i--){
		// If the count is divisble by the groupSize and this is not the last (ie. first) character, add a comma.
		if(count % groupSize == 0 && i != 0){
	return fnum.reverse().join('') + decimalPlaces;

9 Responses

    1. Hey Adam,

      In the expression above, I have defined a variable called “theComma” near the top of the “commas” function. To perform the same grouping of 3 numerals but using a space instead of a comma, you can change that line so that the comma character is replaced with a space character.

      So, you would need to change this line:
      var theComma = “,”;
      to this:
      var theComma = ” “;

    1. Well, this is far too late to help you Joshgun, but hopefully helps others. You will need to edit the expression so that it links to your Slider control. This is as simple as highlighting the example number (123456789) and pick-whipping to the Slider Effect value.

  1. make us of regex to save yourself all that code.

    var count = Math.floor(slider or point control);
    (count.toString()).replace(/\B(?=(\d{3})+\b)/g, “,”);

    doesnt work well with decimal places though

Leave a Reply