Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Number counter expression

  • Number counter expression

    Posted by Nick Doyle on March 4, 2019 at 12:53 pm

    Hello,

    I’m working on a project and I’ve got a basic number counter set up to display the number 1213.37. I’ve used the following expression:

    effect(“Slider Control”)(“Slider”).valuetoFixed(2)

    I need to change the format of the number to this:

    1,213.37

    I’ve used this piece of code to add the coma in:

    var num = effect(“Slider Control”)(“Slider”)
    num = Comma(num);
    [num]

    function Comma(number)
    {
    number = ” + Math.round(number/10);
    if (number.length > 3)
    {
    var mod = number.length % 3;
    var output = (mod > 0 ? (number.substring(0,mod)) : ”);
    for (i=0 ; i < Math.floor(number.length / 3); i++)
    {
    if ((mod == 0) && (i == 0))
    output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
    else
    output+= ‘,’ + number.substring(mod + 3 * i, mod + 3 * i + 3);
    }
    return (output);
    }
    else return number;
    }

    I can’t seem to figure out how to adjust it to move the comma and put the decimal point back in. Any help and advice would be very much appreciated.

    Liran Tabib replied 7 years, 2 months ago 3 Members · 9 Replies
  • 9 Replies
  • Oleg Pirogov

    March 4, 2019 at 1:30 pm

    This:
    var parts = number.toString().split(".");
    gives you an array [‘1213′, ’37’], so you can apply your comma-separating function to parts[0] getting [‘1,213′, ’37’] and then rejoining them with:
    parts.join(".");

    Just for a reference, a regex code which does what you want:
    function numberWithCommas(x) {
    var parts = x.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
    }

    which I’ve found here: https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript

  • Nick Doyle

    March 4, 2019 at 1:43 pm

    Ooo thank you, can’t seem to find the correct point to add this in though. Here is what I tried:

    var num = effect(“Slider Control”)(“Slider”)
    num = Comma(num);
    [num]

    function numberWithCommas(x) {
    var parts = x.toString().split(“.”);
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, “,”);
    return parts.join(“.”);
    }

    function Comma(number)
    {
    number = ” + Math.round(number/10);
    if (number.length > 3)
    {
    var mod = number.length % 3;
    var output = (mod > 0 ? (number.substring(0,mod)) : ”);
    for (i=0 ; i < Math.floor(number.length / 3); i++)
    {
    if ((mod == 0) && (i == 0))
    output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
    else
    output+= ‘,’ + number.substring(mod + 3 * i, mod + 3 * i + 3);
    }
    return (output);
    }
    else return number;
    }

  • Oleg Pirogov

    March 4, 2019 at 1:58 pm

    It’s an alternative to your function, so:

    function numberWithCommas(x) {
    var parts = x.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
    }

    var num = effect("Slider Control")("Slider")
    num = numberWithCommas(num);
    [num]

  • Nick Doyle

    March 4, 2019 at 2:32 pm

    Sorry I’m a bit confused I’ve pasted it in place of my function and now the number turns into the word ‘object’

  • Oleg Pirogov

    March 4, 2019 at 2:48 pm

    Can you post the whole expression?

  • Nick Doyle

    March 4, 2019 at 3:12 pm

    Here it is. I replaced the existing function with below:

    function numberWithCommas(x) {
    var parts = x.toString().split(“.”);
    parts[0] = parts[0].replace(/\B(?=(\d{5})+(?!\d))/g, “,”);
    return parts.join(“.”);
    }

    var num = effect(“Slider Control”)(“Slider”)
    num = numberWithCommas(num);
    [num]

  • Oleg Pirogov

    March 4, 2019 at 3:27 pm

    Yeah, it’s effect(“Slider Control”)(“Slider”).value
    This should work:

    function numberWithCommas(x) {
    var parts = x.toString().split(".");
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    return parts.join(".");
    }

    var num = effect("Slider Control")("Slider").value
    num = numberWithCommas(num);
    [num]

  • Nick Doyle

    March 4, 2019 at 4:21 pm

    Perfect! Thanks so much for your help.

  • Liran Tabib

    May 16, 2019 at 10:36 am

    If you wish to skip the Expressions and you have few buck to spare you can always use Counter Preset

    Cheers!

    Liran Tabib
    http://www.vdodna.com

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