jack challenger

Counting from 1 to millions - add commas

Recommended Posts

Hi,

 

I am counting from 1 to a number as follow, and I was wondering how one would go about putting thousands decimals or space into the number. So that it reads like "6,340,874"?

 

function cx() {
          var counter = { var: 0 };
          var tal = document.getElementById("cx");
              
           TweenMax.to(counter, 1, {
                var: 6340874, 
                onUpdate: function () {
                    tal.innerHTML = Math.ceil(counter.var);
                },
                ease:Circ.easeOut
            });
          }

 

Thanks!

Share this post


Link to post
Share on other sites

That's not really GSAP related question. Most of the javascript related answers can be found with a simple google search, it will be great if all questions asked on this forum are directly or indirectly GSAP related only.

 

 

  • Like 4

Share this post


Link to post
Share on other sites

Hello @jack challenger and Welcome to the GreenSock forum!

 

Here is another way if you need the last two decimal places if its a currency:

 

 

Happy Tweening!

  • Like 2

Share this post


Link to post
Share on other sites

Another tip, when animating numbers, set tabular numbers in your CSS to prevent jumping.

 

Notice how the bottom number doesn't jump when animating.

 

 

  • Like 6

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.