ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Counter Expression with decimal mark

FAQ   •   VIEW ALL
 Counter Expression with decimal mark on Sep 5, 2012 at 7:52:49 am

Hi,

I'm struggling with a counter expression in After Effects (CS 5.5).

The counter is suposed to count linearly from 0 to 15,000. My problem is to get the decimal mark to work. Ex. 15,000 instead of 15000.

My expression looks like this at the moment:

numDec = 0; // digits to right of decimal
numDigit = 0; // digits to left of decimal
if (val < 0) sign = "-" else sign = ""; s = Math.abs(val).toFixed(numDec); while (s.length < numDigit + numDec + 1) s = "0" + s; sign + s

A little help will be highly appreciated.

Cheers,
Lars Rønne

 Re: Counter Expression with decimal markon Sep 5, 2012 at 1:45:47 pm

Are you looking for a counter with a comma? If so I just copied this one from Dan Ebberts.

```startValue = 0; endValue = 15000; dur = 5; //seconds s = "" + Math.round(linear(time,inPoint,inPoint+dur,startValue,endValue)); if (s.length > 6) s.substr(0,s.length-6) + "," + s.substr(-6,3) + "," + s.substr(-3,3) else if (s.length > 3) s.substr(0,s.length-3) + "," + s.substr(-3,3) else s```

Johnny Cuevas, Editor
Thinkck.com

"I have not failed 700 times. I have succeeded in proving that those 700 ways will not work. When I have eliminated the ways that will not work, I will find the way that will work."
---THOMAS EDISON on inventing the light bulb.

 Re: Counter Expression with decimal markon Sep 5, 2012 at 1:47:56 pm

Yes. Exactly, John. You're a hero. Thanks alot!!!

 Re: Counter Expression with decimal markon Sep 25, 2012 at 6:00:00 pm

Hi.

Greatly appreciate the time spent on making this expression, helped me allot.

But i would like to start the counter animation with a keyframe instead of the start being the in.point of the text layer. It would be such a time saver; now i have to make a second and a third text layer just to have them freezed (with the correct number) before the count starts/ends.
Does it make sense?

My other question is how to include different signs (€, \$, +, etc.) infront of the counter instead of in the end like I have it now. I tried being a little creative with the expression and somehow got (almost at least) what I wanted; decimals between every thousend (what I wanted) and "signs" in the end (not what I wanted) of the counter. I accomplished getting the signs infront of the counter on "thousends" though, but the "hundreds" will not show the "signs" infront of them.

startValue = 650;
endValue = 1750;
beginTime = 26.15; // start counting at time = 28.05
dur = 9.5; //seconds
s = "" + Math.round(linear(time,inPoint,inPoint+dur,startValue,endValue));
if (s.length > 6)
s.substr(0,s.length-6) + "." + s.substr(-6,3) + "." + s.substr(-3,3)
else if (s.length > 3)
s.substr(0,s.length-3) + "." + s.substr(-3,3) + " €"
else
s + " €"

Kim Krohn Berle

 Re: Counter Expression with decimal markon Sep 5, 2012 at 2:06:44 pm

this is what I found that helped me: I recently had a project where I needed a counter to go from 1 - 1,061.

http://www.videocopilot.net/forum/viewtopic.php?f=5&t=17819

Formatting a number with commas

1. Create a comp. Name it anything you like.
2. Create a new text layer and name it "dummy" (this layer can be turned off; it feeds a number to the final text layer. I haven't found a way around this; please let me know if there's another way).
3. Add a slider to the "dummy" layer (Effects > Expression Controls > Slider Control). After Effects should automatically name the slider control "Slider"
4. Add an expression to Source Text of "dummy" (Animation > Add Expression).
5. Pickwhip the expression to the "Slider" control from step 3.
6. Create a second text layer. This will be the layer that displays the formatted number.
7. Add an expression to Source Text of the new text layer. Replace the code with the following:

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;
}

Now try changing the value of the "Slider" control. Your visible text layer should reflect that number, now formatted with commas. Two catches: it only works for integers, and the slider tops out at 1,000,000. You should be able to pickwhip to any propery that contains a number (decimals are rounded off automatically).

 Re: Counter Expression with decimal markon Sep 5, 2012 at 2:15:31 pm

Thank you as well, Terrance.