Activity › Forums › Adobe After Effects Expressions › Truncating dynamically generated numbers
-
Truncating dynamically generated numbers
Posted by Ole Sturm on May 22, 2017 at 7:25 amHi all,
I’ve got a situation where I’m having AE generate numbers based on various layer interactions. However, I need to truncate the display of these numbers so that 1,248,000 for example, ends up as 1.25. I’ve looked at QubaHQs ÜberNumber (https://qubahq.com/2014/06/ubernumber/) preset but unfortunately it doesn’t allow for this functionality. Is it possible to use an expression to truncate numbers in this way?
Many thanks
Ole Sturm replied 8 years, 11 months ago 2 Members · 8 Replies -
8 Replies
-
Dan Ebberts
May 22, 2017 at 9:58 amI’m not sure exactly what you’re after–something like this maybe:
n = 1248000; // your number here
(n/1000000).toFixed(2)Dan
-
Ole Sturm
May 22, 2017 at 11:56 amHi Dan,
Long time. Fancy meeting you here ????
Here’s what I’ve got a in a little more detail:
I got the below expression off a very helpful AE Lister. The readout is for an expression-driven timeline which spans 600 million years and the below expression gives me the readout for the year where the marker/pointer is currently sitting. The problem I’m having with figuring this out is as follows:
1- when the readout for the year is 1,240,000.00 I need to truncate it to 1.24.
2- when the readout is 6,000,000 I need to truncate it to 6
3- when the readout is 400,000 I need to truncate it to 400
4- when the readout is 4,000 I need to truncate it to 4This is because the label next to the readout will say “million years ago” and “thousand years ago”
Then, I also need to look at controlling the decimal points. In the fist instance above we do want a decimal point but not in the other 3 cases. Currently I have this snippet “FixValue = effect(“Decimal”)(“Slider”).value;” which is linked to a slider where I keyframe the number of decimals manually.
I’ve been chasing my tail with this variations of this for a week now and don’t even know if it’s possible to resolve using just one expression. In any case, I reckon if you don’t know then nobody does.
Thanks Dan,
Ole
scf = thisComp.layer("CONTROL").effect("scale")("Slider")/10000;
ptPos = thisComp.layer("Marker").transform.position[0];
date = (thisComp.width-ptPos)*1/scf;
date -
Dan Ebberts
May 22, 2017 at 7:38 pmHi Ole,
The trick is being able to come up with an algorithm that accounts for every possibility and can be put into words. Once you have the algorithm, it shouldn’t be too tough to convert it to code. I don’t think I have enough info to get it right, but if you can articulate the exact rules for your truncation, we can probably get there.
Dan
-
Ole Sturm
May 22, 2017 at 8:01 pmHey Dan,
This is sounding rocket-sciency. Here’s what I think you’re after:
10,000,000.N – 600,000,000.N (where N denotes the endless decimals that AE is chucking out) needs to be truncated to whole millions without any decimals eg 10 or 600.
1,000,000.N to 9,999,999.N needs to be truncated to whole millions with 2 decimals. eg 1.00 or 9.99
1,000.N – 999,999.N needs to be truncated to whole thousands without decimals eg 1 or 999.
I think where the millions are concerned there is no rounding required since the timeframe is so large but with the thousands it probably would require rounding to the nearest thousand in which case 999,999 would be 1 (million)I’m not sure whether that’s what you mean but if it helps, here’s a rough of where the timeline’s at which will probably help to put things into perspective:
https://www.dropbox.com/s/zaztp5lzhdyt5ht/SC06-09-02_May21.mp4?dl=0 You’ll notice that the readout is often way out of whack (especially around the 1million mark) which is due to us currently keyframing it.Cheerio
Ole
-
Dan Ebberts
May 22, 2017 at 8:25 pmSomething like this maybe:
function format(n){
if (n >= 10000000){
return (n/10000000).toFixed(0);
}else if (n >= 1000000){
return (n/1000000).toFixed(2)
}else{
return (n/1000).toFixed(0);
}
}
format(1240000);
Dan
-
Ole Sturm
May 22, 2017 at 8:29 pmWow, that was quick. How do I go about combining that with the expression that generates the number ie
scf = thisComp.layer("CONTROL").effect("scale")("Slider")/10000;
ptPos = thisComp.layer("Marker").transform.position[0];
date = (thisComp.width-ptPos)*1/scf;
date -
Dan Ebberts
May 22, 2017 at 9:26 pmI haven’t tested it, but I think it would be like this:
function format(n){
if (n >= 10000000){
return (n/10000000).toFixed(0);
}else if (n >= 1000000){
return (n/1000000).toFixed(2)
}else{
return (n/1000).toFixed(0);
}
}
scf = thisComp.layer("CONTROL").effect("scale")("Slider")/10000;
ptPos = thisComp.layer("Marker").transform.position[0];
date = (thisComp.width-ptPos)*1/scf;
format(date)
Dan
-
Ole Sturm
May 23, 2017 at 4:51 amHi Dan,
That’s done the trick. Thank you so much for the help – you’re like the gift that keeps on giving. I clearly recall the first time I relied on your help all the way back in 2004!!!
Thanks again,
Ole
Reply to this Discussion! Login or Sign Up