ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Number counter expression

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Nick Doyle
Number counter expression
on Mar 4, 2019 at 12:53:55 pm

Hello,

I'm working on a project and I've got a basic number counter set up to display the number 1213.37. I've used the following expression:

effect("Slider Control")("Slider").valuetoFixed(2)

I need to change the format of the number to this:

1,213.37

I've used this piece of code to add the coma in:

var num = effect("Slider Control")("Slider")
num = Comma(num);
[num]

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

I can't seem to figure out how to adjust it to move the comma and put the decimal point back in. Any help and advice would be very much appreciated.


Return to posts index

Oleg Pirogov
Re: Number counter expression
on Mar 4, 2019 at 1:30:00 pm

This:
var parts = number.toString().split(".");
gives you an array ['1213', '37'], so you can apply your comma-separating function to parts[0] getting ['1,213', '37'] and then rejoining them with:
parts.join(".");

Just for a reference, a regex code which does what you want:
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}

which I've found here: https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-comm...


Return to posts index

Nick Doyle
Re: Number counter expression
on Mar 4, 2019 at 1:43:23 pm

Ooo thank you, can't seem to find the correct point to add this in though. Here is what I tried:

var num = effect("Slider Control")("Slider")
num = Comma(num);
[num]

function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}

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


Oleg Pirogov
Re: Number counter expression
on Mar 4, 2019 at 1:58:22 pm

It's an alternative to your function, so:

function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}

var num = effect("Slider Control")("Slider")
num = numberWithCommas(num);
[num]


Return to posts index

Nick Doyle
Re: Number counter expression
on Mar 4, 2019 at 2:32:13 pm

Sorry I'm a bit confused I've pasted it in place of my function and now the number turns into the word 'object'


Return to posts index

Oleg Pirogov
Re: Number counter expression
on Mar 4, 2019 at 2:48:34 pm

Can you post the whole expression?


Return to posts index


Nick Doyle
Re: Number counter expression
on Mar 4, 2019 at 3:12:42 pm

Here it is. I replaced the existing function with below:

function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{5})+(?!\d))/g, ",");
return parts.join(".");
}

var num = effect("Slider Control")("Slider")
num = numberWithCommas(num);
[num]


Return to posts index

Oleg Pirogov
Re: Number counter expression
on Mar 4, 2019 at 3:27:57 pm

Yeah, it's effect("Slider Control")("Slider").value
This should work:

function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}

var num = effect("Slider Control")("Slider").value
num = numberWithCommas(num);
[num]


Return to posts index

Nick Doyle
Re: Number counter expression
on Mar 4, 2019 at 4:21:42 pm

Perfect! Thanks so much for your help.


Return to posts index


Liran Tabib
Re: Number counter expression
on May 16, 2019 at 10:36:12 am

If you wish to skip the Expressions and you have few buck to spare you can always use Counter Preset

Cheers!

Liran Tabib
http://www.vdodna.com


Return to posts index

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