Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Expressions: Leading zeros and decimal places.

  • Expressions: Leading zeros and decimal places.

    Posted by Jakub Michalski on October 30, 2005 at 12:14 pm

    For some time now I have been cracking my head tryign to figure out if there is a way to develop an expression for Text Source that would allow the number (say, driven by a calculation, or a link to slider or another effect) to be presented with leading zeros.

    Of course there is a possibility of including a large set of “if” statements, but I was hoping maybe someone knows an easier way.

    Apart from leading zeros, how about displaying a number to a certain amount of decimal places. How about placing coma separator for thousands etc?

    Say X=5738 Y=4

    is there a simple way of making them display as:

    5738 0004

    or

    5738.00 4.00

    or

    5,738 4

    etc.

    Awesomefighter Toons replied 10 years, 6 months ago 8 Members · 12 Replies
  • 12 Replies
  • Colin Braley

    October 30, 2005 at 4:49 pm

    Here is an expression for source text that should add leading zeroes to a number. Just modify the amtOfZeroes variable and pick whip the nums variable to whatever the source of your number is.

    //–Expression begins
    nums = thisComp.layer(“Deep Lime Green Solid 1”).effect(“Slider Control”)(“Slider”);
    amtOfZeroes = 10;
    //–Do not modify below this line
    isNeg = false;
    if(nums < 0) { nums = Math.abs( nums ); isNeg = true; } sVal = Math.round( nums ) + ""; while(sVal.length < amtOfZeroes) { sVal = "0" + sVal; } if(isNeg) sVal = "-" + sVal; sVal //--Expression ends ~Colin

  • Colin Braley

    October 30, 2005 at 5:00 pm

    Here is a a simple way to round a number to a specified amound of decimal places:
    Just pick whip nums to something else and change the value of the numOfPlaces variable to make it round to a different number of decimal places.

    //–Begin Expression
    nums = thisComp.layer(“Deep Lime Green Solid 1”).effect(“Slider Control”)(“Slider”);
    numOfPlaces = 3;
    //–Do not modify below this line

    function roundTo(number, places)
    {
    num = Math.round ( Math.pow(number, places) );
    num /= Math.pow(10, places);
    return num;
    }

    roundTo(nums, numOfPlaces)

    //–End expression

    ~Colin

  • Jakub Michalski

    October 30, 2005 at 7:02 pm

    Wow, great!

    Would never thought of doing these loops for adding the zeros! Decimal places are perfect as well. Thank you very much – this will help me a lot in debugging my expressions!

  • Colin Braley

    October 30, 2005 at 7:18 pm

    Glad I could help.
    ~Colin

  • Drew Holzinger

    November 13, 2012 at 5:19 am

    Thanks for the expression!
    I’m using 1 decimal point.
    How can i get it to always display the decimal point when the number is whole? e.g. “3.0” or “0.0”

    also, I had to add ‘*10’ to the first line because the script was putting all the digits of the slider value behind the decimal (keyframed from 0.0 to 5.8 over 40 frames)

  • Spencer Tweed

    February 24, 2015 at 11:38 pm

    I need this for a project I’m on so I went ahead and smashed all of these expressions together. Yes, it will also do what you want with the decimal places!

    nums = PICKWHIP_VALUE_HERE;
    amtOfZeroes = 10;
    numOfDecimals = 3;

    //------------------------------------------------------------

    nums = nums.value.toFixed(numOfDicemals);

    isNeg = false;
    if (nums &lt; 0)
    {
    nums = Math.abs(nums);
    nums = nums.toFixed(numOfDecimals);
    isNeg = true;
    }
    numsArray = nums.split(".");
    numsFloat = numsArray[0];
    while (numsFloat.length &lt; amtOfZeroes)
    {
    numsFloat = "0" + numsFloat;
    }
    if(isNeg)
    {
    numsFloat = "-" + "0" + numsFloat;
    }

    numsFloat + "." + numsArray[1]

  • Spencer Tweed

    February 24, 2015 at 11:39 pm

    Nice expressions! I modified a bit to suit my project and smashed your expressions together:

    nums = PICKWHIP_VALUE_HERE;
    amtOfZeroes = 10;
    numOfDecimals = 3;

    //------------------------------------------------------------

    nums = nums.value.toFixed(numOfDicemals);

    isNeg = false;
    if (nums &lt; 0)
    {
    nums = Math.abs(nums);
    nums = nums.toFixed(numOfDecimals);
    isNeg = true;
    }
    numsArray = nums.split(".");
    numsFloat = numsArray[0];
    while (numsFloat.length &lt; amtOfZeroes)
    {
    numsFloat = "0" + numsFloat;
    }
    if(isNeg)
    {
    numsFloat = "-" + "0" + numsFloat;
    }

    numsFloat + "." + numsArray[1]

  • Whitney Doug

    August 20, 2015 at 8:07 pm

    Any idea why this expression would be giving me the error: “Expected: )” on line 7, which I believe is this line: “if (nums < 0)”

  • Dan Ebberts

    August 20, 2015 at 10:15 pm

    I’d guess it’s because the poster previewed the post, which converts all < characters to html code. Dan

  • Spencer Tweed

    August 23, 2015 at 8:04 pm

    Can you post your expression? Can’t tell without seeing the code…

    – Spencer

Page 1 of 2

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy