FORUMS: list search recent posts

Numbers Expression Replace commas with a space....

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Ross Hammond
Numbers Expression Replace commas with a space....
on Jan 22, 2015 at 4:07:27 pm

Hey folks see below is an expression I have for a counter which works perfectly, However I would like to replace the commas with a space if possible as this is a request for reversioning into a different language can someone point me in the direction of how to do this,

would it be in the commas not requested section.....

Thank you so much.

Regards Chossy.

num = thisComp.layer("Controls").effect("number")("Slider")*100000;
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 >= 0; i-- ){
finalResult = str.charAt( i ) + finalResult;
if( (str.length - i) % 3 == 0 && 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
}


The last true family man


Return to posts index

Dan Ebberts
Re: Numbers Expression Replace commas with a space....
on Jan 22, 2015 at 5:18:52 pm

I haven't tried it, but I think you could just replace this line:

finalResult = "," + finalResult;

with this:

finalResult = " " + finalResult;


Dan



Return to posts index

Declan Smith
Re: Numbers Expression Replace commas with a space....
on Jan 22, 2015 at 5:32:35 pm

You just need to change the line:

finalResult = ',' + finalResult;

to

finalResult = ' ' + finalResult;


Or you could make this more configurable by having another slider to control what separator character you want and using the changes as in the script below, Basically the two lines with the word 'separator' in is what you need.

To use this, just put the slider to the ascii code of the required character (don't go over 255!). For a space, put the slider at 32 and for a comma 44.

num = thisComp.layer("Controls").effect("number")("Slider")*100000;
amtOfDec = thisComp.layer("Controls").effect("amt_of_decimals")("Slider");
commas = thisComp.layer("Controls").effect("use_commas")("Checkbox");
separator = String.fromCharCode(Math.ceil(thisComp.layer("Controls").effect("separator")("Slider")));
//--
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 >= 0; i-- ){
finalResult = str.charAt( i ) + finalResult;
if( (str.length - i) % 3 == 0 && i != 0 )
finalResult = separator + 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
}


Declan Smith
http://www.madpanic.tv
After Effects CS6/ FCS3 / Canon XLH1 / Canon 7D / Reason / Cubase

"it's either binary or it's not"


Return to posts index


Ross Hammond
Re: Numbers Expression Replace commas with a space....
on Jan 22, 2015 at 11:04:16 pm

Thank you very much for the responses, looks pretty straightforward. I'll give it a go tomorrow at work.

Thank you so much guys.

Regards Ross.

The last true family man


Return to posts index

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