ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Expression: forcing decimals on even integers?

FAQ   •   VIEW ALL
 Expression: forcing decimals on even integers? on Apr 17, 2013 at 8:24:59 pm

Is there a way to modify an expression like this:

-thisComp.layer("shape").transform.rotation.value.toFixed(2) + "";

so that there are always decimals even with even integers. For instance, the current expression shows 3 as 3 instead of 3.00, or 3.5 as 3.5 instead of 3.50.

 Re: Expression: forcing decimals on even integers?on Apr 18, 2013 at 3:50:54 pm

Possible with a bit of string manipulation.

n is the original number, nScaled is the original number * 100 and rounded (so we can ignore everything after the decimal place), and s is the string we will manipulate from nScaled.

// get the value we want
n = thisComp.layer("shape").transform.rotation.value;

nScaled = Math.round(n * 100);

// make it an unsigned string -- we'll add the negative back later if it's supposed to be negative
s = "" + Math.abs(nScaled.toString());

// pad low values with leading zeros to ensure at least 3 digits after we move the decimal point
while (s.length < 3)
{
if (n<0) s = "" + nScaled.toString();
s = "0" + s;
}

// if it was negative, add the - sign back
if (n<0) s = "-" + s

// insert the decimal place before the last two decimal places
s = s.substr(0,s.length-2) + "." + s.substr(s.length-2,s.length)

Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events

 Re: Expression: forcing decimals on even integers?on Apr 18, 2013 at 4:40:06 pm

Thanks Walter. Your expression is the closest yet I have found. But when the number is between 0 and -1 I see things like -0-.43 and -0.-1

Seems like the negative sign creates issues for many of the expression variations I have played with... not sure why this is.

 Re: Expression: forcing decimals on even integers?on Apr 18, 2013 at 4:44:10 pm

Oops!

Try this:

// get the value we want
n = thisComp.layer("shape").transform.rotation.value;

// add two zeros, and make it an unsigned string -- we'll add the negative back later if it's supposed to be negative
nScaled = Math.abs(Math.round(n * 100));
s = "" + nScaled.toString();

// pad low values with leading zeros to ensure at least 3 digits after we move the decimal point
while (s.length < 3) s = "0" + s;

// if it was negative, add the - sign back
if (n<0) s = "-" + s

// insert the decimal place before the last two decimal places
s = s.substr(0,s.length-2) + "." + s.substr(s.length-2,s.length)

Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events

 Re: Expression: forcing decimals on even integers?on Apr 18, 2013 at 4:48:18 pm

I edited this after posting -- James, please check the forum for the latest.

Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events

 Re: Expression: forcing decimals on even integers?on Apr 18, 2013 at 4:53:53 pm

Now I am getting a short spinning beachball, and then the AE expression warning: Timeout while waiting for the engine. Expression disabled.

Error occurred at line 12.

 Re: Expression: forcing decimals on even integers?on Apr 18, 2013 at 4:58:11 pm

I see I was missing a semicolon at the end of line 12 -- does adding it back in correct it for you?

Sorry for my sloppiness.

// get the value we want
n = thisComp.layer("shape").transform.rotation.value;

// add two zeros, and make it an unsigned string -- we'll add the negative back later if it's supposed to be negative
nScaled = Math.abs(Math.round(n * 100));
s = "" + nScaled.toString();

// pad low values with leading zeros to ensure at least 3 digits after we move the decimal point
while (s.length < 3) s = "0" + s;

// if it was negative, add the - sign back
if (n<0) s = "-" + s;

// insert the decimal place before the last two decimal places
s = s.substr(0,s.length-2) + "." + s.substr(s.length-2,s.length)

Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events

 Re: Expression: forcing decimals on even integers?on Apr 18, 2013 at 5:08:08 pm

That works! Really appreciate it. I was starting to get obsessed...

 Re: Expression: forcing decimals on even integers?on Oct 4, 2013 at 11:09:40 am

BRILLIANT

HI.
I added this to a text script, sorry can't remember whose, maybe greymachine or Alvarez, but this means I can now count up, with a percent sign and the sign not move around:

n = effect("Text VAlue")("Slider");
// add two zeros, and make it an unsigned string -- we'll add the negative back later if it's supposed to be negative
nScaled = Math.abs(Math.round(n * 100));
s = "" + nScaled.toString();
// pad low values with leading zeros to ensure at least 3 digits after we move the decimal point
while (s.length < 3) s = "0" + s;
// if it was negative, add the - sign back
if (n<0) s = "-" + s;
// insert the decimal place before the last two decimal places
s = s.substr(0,s.length-2) + "." + s.substr(s.length-2,s.length)+"%"

THANKS EVERYONE

Tris