FORUMS: list search recent posts

commas and decimal points in source text expression

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Fadi Baqi
commas and decimal points in source text expression
on Mar 6, 2013 at 1:19:32 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;
}


Return to posts index

Fadi Baqi
Re: commas and decimal points in source text expression
on Mar 6, 2013 at 4:33:12 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
}


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2018 CreativeCOW.net All Rights Reserved
[TOP]