ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Expressions: Leading zeros and decimal places.

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Jakub Michalski
Expressions: Leading zeros and decimal places.
on Oct 30, 2005 at 12:14:04 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.


Return to posts index

Colin Braley
Re: Expressions: Leading zeros and decimal places.
on Oct 30, 2005 at 4:49:56 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


Return to posts index

Jakub Michalski
Re: Expressions: Leading zeros and decimal places.
on Oct 30, 2005 at 7:02:04 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!


Return to posts index


Colin Braley
Re: Expressions: Leading zeros and decimal places.
on Oct 30, 2005 at 7:18:55 pm

Glad I could help.
~Colin


Return to posts index

Colin Braley
Re: Expressions: Leading zeros and decimal places.
on Oct 30, 2005 at 5:00:37 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


Return to posts index

Drew Holzinger
Re: Expressions: Leading zeros and decimal places.
on Nov 13, 2012 at 5:19:24 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)


Return to posts index


Spencer Tweed
Re: Expressions: Leading zeros and decimal places.
on Feb 24, 2015 at 11:38:14 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]


Return to posts index

Spencer Tweed
Re: Expressions: Leading zeros and decimal places.
on Feb 24, 2015 at 11:39:30 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]


Return to posts index

Whitney Doug
Re: Expressions: Leading zeros and decimal places.
on Aug 20, 2015 at 8:07:37 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)"



Return to posts index


Dan Ebberts
Re: Expressions: Leading zeros and decimal places.
on Aug 20, 2015 at 10:15:50 pm
Last Edited By Dan Ebberts on Aug 20, 2015 at 10:16:47 pm

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

Dan



Return to posts index

Spencer Tweed
Re: Expressions: Leading zeros and decimal places.
on Aug 23, 2015 at 8:04:35 pm

Can you post your expression? Can't tell without seeing the code...

- Spencer


Return to posts index

Quba Michalski
Re: Expressions: Leading zeros and decimal places.
on Aug 23, 2015 at 11:35:39 pm

If you want to continue learning expressions, disregard this reply. If you, however just want to get things done, I recommend the free expression called UberNumber I created a while back.

http://qubahq.com/2014/06/ubernumber/

It does leading zeroes, decimal places, negative symbol before/after currency symbol, digit grouping and a few more things. The idea was to write a number expression to end all expressions and I think it works.

Quba Michalski
qubahq.com


Return to posts index

AwesomeFighter Toons
Re: Expressions: Leading zeros and decimal places.
on Oct 27, 2015 at 3:05:37 am

Hi everyone! If you've stumbled upon this post and found the previous expressions don;t work then use this modified one. There was a slight typo in the last post which breaks the expression.

nums= PICKWHIP VALUE ;
amtOfZeroes= 10 ;
numOfDecimals= 3 ;

//------------------------------------------------------------ ;

nums = nums.value.toFixed(numOfDecimals);

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]


Return to posts index

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