Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions commas and decimal points in source text expression

  • commas and decimal points in source text expression

    Posted by Fadi Baqi on March 6, 2013 at 1:19 pm

    I’m using an expression to add commas to numbers but I also want to add a decimal point and two fixed decimal integers.

    This is the expression I am using. Can anyone help?

    var num = thisComp.layer("dummy").text.sourceText;
    num = Comma(num);
    [num]

    function Comma(number)
    {
    number = '' + Math.round(number);
    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;
    }

    Fadi Baqi replied 13 years, 2 months ago 1 Member · 1 Reply
  • 1 Reply
  • Fadi Baqi

    March 6, 2013 at 4:33 pm

    No worries. Found a script that works better over here:

    num = thisComp.layer("Controls").effect("number")("Slider");
    amtOfDec = thisComp.layer("Controls").effect("amt_of_decimals")("Slider");
    commas = thisComp.layer("Controls").effect("use_commas")("Checkbox");
    //--
    num = num + 0;
    amtOfDec = amtOfDec + 0;
    commas = commas == 1;
    //--
    if(! commas){
    //commas not requested
    num.toFixed( amtOfDec );
    }else{
    //commas requested

    //This function takes a positive whole number as a string
    //and adds commas to it
    function addCommas( str ){
    finalResult = "";
    for( i = str.length - 1; i &gt;= 0; i-- ){
    finalResult = str.charAt( i ) + finalResult;
    if( (str.length - i) % 3 == 0 &amp;&amp; i != 0 )
    finalResult = "," + finalResult;
    }
    return finalResult;
    }
    //--
    intPart = Math.floor( Math.abs( num ) );
    decPart = Math.abs(num) - intPart;
    wasNeg = num &lt; 0;
    result = "";
    if( wasNeg )
    result = "-" + result;
    intPartString = intPart + "";
    decPartString = decPart.toFixed( amtOfDec ) + "";
    decPartString = decPartString.substring( 1 );
    result = result + addCommas( intPartString ) + decPartString;
    result
    }

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