bfintal / Counter-Up

Counter-Up is a lightweight jQuery plugin that counts up to a targeted number when the number becomes visible.
http://bfintal.github.io/Counter-Up/demo/demo.html
GNU General Public License v2.0
746 stars 722 forks source link

24 counts up to 23 and previously posted solutions don't work. #75

Open temkaes opened 4 years ago

temkaes commented 4 years ago

24.0 works, 23 works and 25 works but 24 gets stuck at 23. Interestingly, the same issue can be replicated at 3, 6, 12, 24, 48 and 96...

I tried a few of the earlier posted solutions but still doesn't work.

fastcatch commented 3 years ago

The problem is that first num is divided by divisions and only then multiplied by i. Javascript calculates with floats and thus rounds in sometimes unexpected ways. Change the order (first multiply, then divide) and it's gonna be allright. (Or round the result before parseInting.)

gabrieljenik commented 3 years ago

My fix...

               // For first iteration, don't divide.
                var auxNum = i == divisions ? num : num / divisions * i;

then...

                // Preserve as int if input was int
                var newNum = parseInt(auxNum);

                // Preserve float if input was float
                if (isFloat) {
                    newNum = parseFloat(auxNum).toFixed(decimalPlaces);
                }