Jump to content
Search Community

adding a '0' to scrolling counter with scrollTrigger

Adam.shires test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

Hey guys,


Have been loving learning all about scroll animations and am currently applying a huge amount to a clients website. I've recently managed to amend a number counter - scrollTrigger animation to only go to a single decimal point. But I was wondering how I could add a '0' in front of the single digits before it hits ten? 

Any help would be greatly appreciated 😄

See the Pen ExEEXNe by Geeza82 (@Geeza82) on CodePen

Link to comment
Share on other sites

  • Solution

Here's a modified version of the helper function in the docs that'll add commas, limit it to a certain number of decimals, and pad it with a minimum number of whole value placements: 

function formatNumber(value, decimals, minWhole) {
  let s = (+value).toLocaleString('en-US').split(".");
  minWhole && s[0].length < minWhole && (s[0] = "0000000000".substr(0, minWhole - s[0].length) + s[0]);
  return decimals ? s[0] + "." + ((s[1] || "") + "00000000").substr(0, decimals) : s[0];

See the Pen mdxxwYO?editors=0010 by GreenSock (@GreenSock) on CodePen


Does that help?

  • Like 1
Link to comment
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.
  • Create New...