How to increment number using animate with comma using jQuery?
Asked Answered
U

3

11

I'm trying to increment a number inside an element on page. But I need the number to include a comma for the thousandth place value. (e.g. 45,000 not 45000)

<script>
// Animate the element's value from x to y:
  $({someValue: 40000}).animate({someValue: 45000}, {
      duration: 3000,
      easing:'swing', // can be anything
      step: function() { // called on every step
          // Update the element's text with rounded-up value:
          $('#el').text(Math.round(this.someValue));
      }
  });
</script>
<div id="el"></div>

How can I increment a number using animate with comma?

Ung answered 26/4, 2013 at 2:36 Comment(3)
Have a look at this https://mcmap.net/q/335149/-jquery-number-formattingLaue
I would store the number in a data attribute, then show the comma formatted version of the number in the element. When you increment it, just increase the data version and replace the formatted version.Definitely
This might help you - #1991012Vallery
E
45

Working Demo http://jsfiddle.net/4v2wK/

Feel free to change it more for your need, you can also look at the currency formatter, Hope this will fit your need :)

Code

// Animate the element's value from x to y:
  var $el = $("#el"); //[make sure this is a unique variable name]
  $({someValue: 40000}).animate({someValue: 45000}, {
      duration: 3000,
      easing:'swing', // can be anything
      step: function() { // called on every step
          // Update the element's text with rounded-up value:
          $el.text(commaSeparateNumber(Math.round(this.someValue)));
      }
  });

 function commaSeparateNumber(val){
    while (/(\d+)(\d{3})/.test(val.toString())){
      val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
    }
    return val;
  }

**output* enter image description here

Electropositive answered 26/4, 2013 at 3:10 Comment(4)
any idea why the number is not the same? I am taking about the circle around the output.Koerner
Hiya @AdrianFlorescu umm how do you mean, sorry not sure what are you asking? :) you mean why the output is not say for example: 44,444? like this: jsfiddle.net/hVpqDElectropositive
@Electropositive exactly, but looks like this is because of the animate steps. I replaced the value on complete with the original value. complete : function(){ where.text(endNr); } jsfiddle.net/5yBt8/10Koerner
You should also add a complete function as such: step:function(){ //.. }, complete:function(){ $el.text(commaSeparateNumber(Math.round(this.someValue))); }Insectivorous
I
12

You should also add a complete function as such:

step:function(){
    //..
},
complete:function(){
    $el.text(commaSeparateNumber(Math.round(this.someValue)));
}

Updated Fiddle: Demo

Insectivorous answered 18/7, 2014 at 8:11 Comment(2)
Why? I'm fairly certain that the step function will be called even on the last animation step. The complete function is used to attach a callback to run after all steps are completed.Raffle
Sometimes it wasnt showing a good last result, it was random at the end, sometimes 45000, sometimes 4995 and so on, so i've added this function, and now it works just fine. Thanks!Hautesavoie
H
0

Had the same problem and used toLocaleString to put commas As suggested in the above answer I found the result deviating from the real value and I solved that by adding the complete callback function that is called when the animation is finished

value_to_be_shown=100000000
// Animate a value from 0 to `value_to_be_shown`
$({
    someValue: 0 // Starting value
}).animate({
    someValue: value_to_be_shown // Ending value
}, {
    duration: 1000, // Animation duration in milliseconds
    easing: 'swing', // Easing function to control acceleration of the animation
    step: function () { // Function to be called on every step of the animation
        // Update the element's text with the rounded-up value of `someValue`:
        $('#el_id').text(Math.round(this.someValue).toLocaleString('en-US'));
    },
    complete: function () { // Function to be called after the animation completes
        // Update the element's text with the final value
        $('#el_id').text(value_to_be_shown.toLocaleString('en-US'));
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<div style="background-color:gray;width:300px;text-align:center;height:100px;padding:20px;margin:20vh auto;">
<div style="font-size:2rem;font-weight:bold" id="el_id">0</div>
</div>
Hezekiah answered 7/5, 2023 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.