Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Number expression with decimals

  • Number expression with decimals

  • Javier Genoves

    May 27, 2019 at 11:05 am

    Hello guys.
    Need a help with an expression…

    I found an expression useful for my effects were a number increase until it reach the value designed in last keyframe.
    Now, my problem is that i see that dont allow to choose decimals.

    What part of the expression have to change for obtaining two decimals.?

    Another that i have is how to hide layers with a slider.
    For example, i have 10 layers and i need only to show 5, How to do with a slider?

    Thanks in advanced people, im very new to expressions.
    Regards!

    num = parseFloat(Math.round(thisComp.layer("Number_Control").effect("Number_1")("ADBE Slider Control-0001")));
    str = isNaN(num) ? "" :(num + "");
    str.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ');

  • Tomas Bumbulevičius

    May 27, 2019 at 5:28 pm

    Hey Javier, for numbers, change first line to the following:

    num = parseFloat(thisComp.layer("Number_Control").effect("Number_1")("ADBE Slider Control-0001")).toFixed(2);

    Removed rounding and added toFixed(2), where number in brackets reflects decimal nrs count.

    As for the hiding of layers, its optional, and you can do it in many ways.

    For example, you could apply opacity expression for all layers, where if Slider’s value is higher than text layer’s index, items opacity is set to 0. This is assuming, that your text layers are at the top of layer stacks, and you need to hide them one after another in sequential order, where slider goes from 0 to 10. Again, its trivial 🙂

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.
    Boxer – Dynamic Text Boxes Template with a Live Preview

  • Javier Genoves

    May 28, 2019 at 9:02 am

    Thanks for your help Tomas.

    I will try later what you told me about opacity but i have an error using your expression…
    The expressions works ok when im using to show number 1, but when it have to show other values higher than 2, it show always 1 and its decimals..
    For example: bar has a value of 1.45…the bar increase and the number increase ok
    bar 2 has a value of 8,45..the bar increase but number stop at end of animation at 1.45
    What im doing bad?
    Thanks!!!

  • Tomas Bumbulevičius

    May 28, 2019 at 2:42 pm

    Javier, are you animating the slider from one value to the other? On my end it works as expected.

    On the other hand, why do you need that regex expression? Slider generates only numeric values anyways, so ‘sNaN’ check doesn’t seem to be needed here. Not sure about the replace part.
    str = isNaN(num) ? "" :(num + "");
    str.replace(/(\d)(?=(\d\d\d)+([^\d]|$))/g, '$1 ');

    It generates identical results with & without it, just with a single line of ‘num’ value – wonder whether it has any specifics in your execution.

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.
    Boxer – Dynamic Text Boxes Template with a Live Preview

  • Javier Genoves

    May 29, 2019 at 8:00 am

    Mmm, Im sorry. As i told, im very new about expression so i copy and paste one expression that i found into my project and worked well until i needed decimals.
    I cant explain anything about that regex expression so i thought that is better to upload it for you,
    https://www32.zippyshare.com/v/4koZWt4x/file.html
    The color control expression was made by me, but the number and bar increase isnt.
    Regards and thanks for ur patience.

  • Tomas Bumbulevičius

    May 29, 2019 at 8:19 am

    Javier, thanks for sharing. With expression applied, here is the result. The project you shared doesn’t have updated expression for number like it was suggested, though. With that changed, I suppose it works as expected.

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.
    Boxer – Dynamic Text Boxes Template with a Live Preview

  • Javier Genoves

    May 30, 2019 at 7:24 am

    Thanks Tomas!!! I did finally.

    The sample i sended to you havent updated because i wanted that you can see original code.
    My mistake was not to remove the other lines, just only with your expression works ok, i dont know the function of the other lines.
    But after i update my project, i notice a problem, not about your expression, its cool…its about because i have over 15 bars and sometimes they want decimals and sometines not, so, how can i select with my slider when i want decimals or not instead of editing all numbers expression?
    Best regards!!!

  • Tomas Bumbulevičius

    May 30, 2019 at 8:31 am

    Javier, glad it worked out. Now if you need to have ‘master’ control of the decimals, heres a solution:

    1. Add checkbox expression control where you see it fit your needs (it does not matter where you will put it, as long as its comfortable to edit)
    2. In your text layer’s source expression, add the following expression:

    decimalsStatus = effect("Checkbox Control")(1); //Update the path to your created Checkbox control
    if (decimalsStatus == true){
    decimals = 2;
    }else{
    decimals = 0;
    }
    num = parseFloat(thisComp.layer("Number_Control").effect("Number_1")("ADBE Slider Control-0001")).toFixed(decimals);

    3. If checked, you will have 2 decimals. If not – none decimal values.

    If this needs to be done for every bar number – you have guessed it, it will require to create N of such controllers and link them in texts sources.

    Find out more:
    After Effects Tutorials: motion design, expressions, scripting.
    Boxer – Dynamic Text Boxes Template with a Live Preview

  • Javier Genoves

    June 4, 2019 at 11:27 am

    Thanks Tomas!!!

    I got an error because i dint know how to update my control (as i said, im very noob), I tried not to bother more with my newbie question,so today, finally i found the problem and works as i wanted.
    Now im going to create slider opacity as you told me.
    Hope i can do myself alone.
    Best regards Tomas and really apreciate you help 🙂

Viewing 1 - 9 of 9 posts

Log in to reply.

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