Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expression: forcing decimals on even integers?

  • Expression: forcing decimals on even integers?

    Posted by James Culbertson on April 17, 2013 at 8:24 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.

    Tristan Summers replied 12 years, 10 months ago 3 Members · 8 Replies
  • 8 Replies
  • Walter Soyka

    April 18, 2013 at 3:50 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;

    // add two zeros
    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

  • James Culbertson

    April 18, 2013 at 4:40 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.

  • Walter Soyka

    April 18, 2013 at 4:44 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

  • Walter Soyka

    April 18, 2013 at 4:48 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

  • James Culbertson

    April 18, 2013 at 4: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.

  • Walter Soyka

    April 18, 2013 at 4:58 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

  • James Culbertson

    April 18, 2013 at 5:08 pm

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

  • Tristan Summers

    October 4, 2013 at 11:09 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

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