Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Control the number of Decimal points of a random number with a slider control

  • Control the number of Decimal points of a random number with a slider control

    Posted by Nawaz Alamgir on March 4, 2019 at 1:20 pm

    Im using

    seedRandom(index,true)
    random(0,100)

    to create a random number, is there a way to control the number of decimal points the random number outputs with a slider control?

    I thought linking a slider using .toFixed(x) would work but it doesn’t

    Anyone?

    Kalleheikki Kannisto replied 7 years, 4 months ago 3 Members · 4 Replies
  • 4 Replies
  • Nawaz Alamgir

    March 4, 2019 at 1:34 pm

    I managed to figure it out.

    2 sliders one called value which generates the number with this in the source

    seedRandom(index, true)
    random(0,100)

    and another slider with the code

    places = effect("Number of Decimals")("Slider");
    val = effect("Value")("Slider"); //sub in the name of your slider here
    factor = Math.pow(0.1, places) ;
    sign = "";
    if (val < 0) sign = "-";
    val = Math.abs(val);
    whole = Math.floor(val);
    fraction = "" + Math.round((val - whole)/factor)*factor;
    for (fraction; fraction.length < places+2; fraction+="0");

    sign + whole + "." + fraction.substring(2, places+2)

  • Nawaz Alamgir

    March 4, 2019 at 2:00 pm

    But, if you have a better way of doing this please let me know

  • Andrei Popa

    March 4, 2019 at 3:51 pm

    Your code is rather complicated. If you have a random number and want to round it to x decimals, just go

    var myNumber = someRandomNumber;
    var multiplicator = Math.pow(10,x);
    Math.round(myNumber*multiplicator)/multiplicator;

    To simple things up, multiplicator is 10 if you want a decimat, 100 if you want 2 etc.

    Andrei
    My Envato portfolio.

  • Kalleheikki Kannisto

    March 6, 2019 at 9:35 am

    Or
    parseFloat(random(100)).toFixed(Math.round(*pick-whip slider value here*));

    Kalleheikki Kannisto
    Senior Graphic Designer

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