ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Number counter expression

FAQ   •   VIEW ALL 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. Re: Number counter expressionon 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 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 = parts.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... Re: Number counter expressionon 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 = parts.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;
} Re: Number counter expressionon 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 = parts.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}

var num = effect("Slider Control")("Slider")
num = numberWithCommas(num);
[num] Re: Number counter expressionon 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 = parts.replace(/\B(?=(\d{5})+(?!\d))/g, ",");
return parts.join(".");
}

var num = effect("Slider Control")("Slider")
num = numberWithCommas(num);
[num] Re: Number counter expressionon 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 = parts.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}

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