Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects 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.

    Quba Michalski replied 11 years, 7 months ago 6 Members · 18 Replies
  • 18 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

  • Navarro Parker

    November 29, 2012 at 2:46 am

    Found this during a Google search and I’m getting odd results.

    I have a slider set to “50.00”, but the expression is displaying “125” (no decimals or trailing zeros). I set it to “15.00” and I get “3.375”. I set the slider to “20.00”, and it displays “8”.

    I realize when this was first posted, everyone was on v6.5, not CS6. Has expressions changed that much?

  • Navarro Parker

    November 29, 2012 at 2:48 am

    This expression does not handle decimals under CS6. It rounds to the nearest whole number. Could you possibly modify it to work with current expressions?

  • Dan Ebberts

    November 29, 2012 at 4:48 am

    It works as expected for me in CS6.

    Dan

  • Navarro Parker

    January 1, 2013 at 12:50 am

    That’s so weird. Here’s what I’m getting with CS6 11.0.2

  • Dan Ebberts

    January 1, 2013 at 1:05 am

    I think I’d just do it like this:

    num = thisComp.layer(“Deep Lime Green Solid 1”).effect(“Slider Control”)(“Slider”).value;
    numOfPlaces = 3;
    num.toFixed(numOfPlaces);

    Dan

  • Navarro Parker

    January 1, 2013 at 1:12 am

    Yeah, but then you are losing the leading zeros from the original expression. (but I never got it to work)

Page 1 of 2

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