ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

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

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Paul Connors
Ignore the percent symbol (%) in Source Text when using it for calculations
on Sep 26, 2019 at 7:43:30 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!


Return to posts index

Dan Ebberts
Re: Ignore the percent symbol (%) in Source Text when using it for calculations
on Sep 26, 2019 at 7:52:40 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



Return to posts index

Paul Connors
Re: Ignore the percent symbol (%) in Source Text when using it for calculations
on Sep 26, 2019 at 11:24:46 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!


Return to posts index


Dan Ebberts
Re: Ignore the percent symbol (%) in Source Text when using it for calculations
on Sep 26, 2019 at 11:26:40 pm

For decimals, you need parseFloat():

txt = "37.25%";
parseFloat(txt);


Dan



Return to posts index

Paul Connors
Re: Ignore the percent symbol (%) in Source Text when using it for calculations
on Sep 26, 2019 at 11:29:16 pm

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


Return to posts index

Tomas Bumbulevičius
Re: Ignore the percent symbol (%) in Source Text when using it for calculations
on Sep 27, 2019 at 9:02:40 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


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]