Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Ignore the percent symbol (%) in Source Text when using it for calculations

  • Ignore the percent symbol (%) in Source Text when using it for calculations

    Posted by Paul Connors on September 26, 2019 at 7:43 pm

    Hi Guys. I’m using the source text from a text layer to do some calculations. Everything works great when just using numbers as the input (for example “37”) but the system breaks when I input a percentage (for example “37%”).

    Does anyone know if there is a way to only focus on the numbers in the source text or a way to ignore the percent symbol?

    Thanks!

    Tomas Bumbulevičius replied 6 years, 7 months ago 3 Members · 5 Replies
  • 5 Replies
  • Dan Ebberts

    September 26, 2019 at 7:52 pm

    parseInt() will stop when it gets to a character that it can’t convert to a number. So if you do this:

    txt = “37%”;
    parseInt(txt,10);

    The result will be 37.

    Dan

  • Paul Connors

    September 26, 2019 at 11:24 pm

    Hi Dan. That worked like a charm. But I’ve run into another issue. What if I need it to say “37.25%” and include those decimal places?

    I tried your expression on “37%” and it worked great (it created “37”) but when I tried it on “37.25%” it output “37” as well.

    Thanks!

  • Dan Ebberts

    September 26, 2019 at 11:26 pm

    For decimals, you need parseFloat():

    txt = “37.25%”;
    parseFloat(txt);

    Dan

  • Paul Connors

    September 26, 2019 at 11:29 pm

    Fantastic. Works like a charm! Thank you very much!

  • Tomas Bumbulevičius

    September 27, 2019 at 9:02 am

    And in addition to this, in case if you need specific number of decimals, (like one, or four, or whatever), add .toFixed(x), where x will generate amount of decimal values.

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

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