Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions I want to put decimal when reach more than 1000 (1K), how should I put it?

  • I want to put decimal when reach more than 1000 (1K), how should I put it?

    Posted by Edison Kertawijaya on July 10, 2023 at 1:35 pm

    Currently I’m creating expression when reach 1000, it’s become 1K, but I can’t add decimal, currently I need to add 2 decimal behind:

    numSlider= effect(“Slider Control”)(“Slider”).value.toFixed(2);

    if(numSlider>999 && numSlider<1000000){

    n = numSlider/1000;

    Math.floor(n)+”k”

    }

    else if(numSlider>1000000){

    m = numSlider/1000000;

    Math.floor(m)+”M”

    }

    else(numSlider)

    Filip Vandueren replied 3 years, 1 month ago 3 Members · 3 Replies
  • 3 Replies
  • Dan Ebberts

    July 10, 2023 at 1:46 pm

    Try this:

    numSlider= effect("Slider Control")("Slider").value.toFixed(2);
    numSlider.replace(/\B(?=(\d{3})+(?!\d))/g,",");

    Edit: Sorry – I misread the question–for some reason I thought you wanted to add commas to large numbers.

    Maybe like this?

    n = effect("Slider Control")("Slider").value;
    if (n >= 1000000){
    Math.floor(n/1000000) + n.toFixed(2).substr(-3) + "M";
    }else{
    Math.floor(n/1000) + n.toFixed(2).substr(-3) + "K"
    }

  • Dan Ebberts

    July 10, 2023 at 2:20 pm

    Still not quite right. Should be closer:

    n = effect("Slider Control")("Slider").value;
    if (n >= 1000000){
    Math.floor(n/1000000) + n.toFixed(2).substr(-3) + "M";
    }else if (n >= 1000){
    Math.floor(n/1000) + n.toFixed(2).substr(-3) + "k"
    }else{
    n.toFixed(2);
    }

  • Filip Vandueren

    July 11, 2023 at 9:38 am

    I would leverage Javascript’s own method for formatting numbers like this:

    numSlider= effect("Slider Control")("Slider").value;
    new Intl.NumberFormat('en-US', {notation: 'compact', maximumFractionDigits: 2}).format(numSlider);

    A possible downside is that it uses rounding and not floor, so 1599 is 1.6K and 1609 is 1.61K

    The positive thing is that it automatically does K, M, B, T and if you select a different language/region, it will use the correct for that region.

    For example use ‘ms’ instead of ‘en-US’ and it will be K, J, B, T for Malaysian ‘juta’

    use ‘fr-FR’ and you’ll get k M Md Bn (the French convention) but use ‘fr-CA’ and you get k M G T (the Canadian convention)

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