I am working on simple script that should animate given value (for example `6345.23`) to `0` by counting it down, it should also end up at `0` if specified amount of time have passed (for example `2 seconds`.

I started by simple logic:

* given config: `initial value`, `time in sec`, `interval`
* time is given in seconds so convert it to milliseconds
* calculate amount of ticks by dividing time in ms by interval
* calculate amount of decreased value per tick by dividing initial value by amount of ticks

once above are known we can simply do: (simple model, not actual code)

```
intId = setInterval(function() {
    if(ticks_made === amount_of_ticks) {
        clearInterval(intId);
    } else {
        value -= amount_per_tick;
        // update view
    }
}, interval);

```

actual code:

```
var value = 212.45,
    time = 2, // in seconds
    interval = 20; // in milliseconds

var time_to_ms = time * 1000,
    amount_of_ticks = time_to_ms / interval,
    amount_per_tick = (value / amount_of_ticks).toFixed(5);

var start_time = new Date();

var ticks_made = 0;

var intId = setInterval(function() {

    if(ticks_made === amount_of_ticks) {
        console.log('start time', start_time);
        console.log('end time', new Date());
        console.log('total ticks: ', amount_of_ticks, 'decresed by tick: ', amount_per_tick);
        clearInterval(intId);
    } else {
        value = (value - amount_per_tick).toFixed(5);
        console.log('running', ticks_made, value);
    }

    ticks_made++;

}, interval);

```

Link do [fiddle](https://jsfiddle.net/cnxg83pn/) (in console you can observe how it works)

If you set time to `2 (2 seconds)` its ok, but if you set time to for example `2.55 (2.55 seconds)` it doesnt stop at all at `0`, its passing by and going indefinitely in negative values.

How i can fix it so no matter what is set in seconds its always go precisly one by one until reaches perfectly `0`?

```js
var value = 212.45,
 time = 2, // in seconds
 interval = 20; // in milliseconds

var time_to_ms = time * 1000,
 amount_of_ticks = time_to_ms / interval,
 amount_per_tick = (value / amount_of_ticks).toFixed(5);

var start_time = new Date();

var ticks_made = 0;

var intId = setInterval(function() {

 if(ticks_made === amount_of_ticks) {
  console.log('start time', start_time);
  console.log('end time', new Date());
  console.log('total ticks: ', amount_of_ticks, 'decresed by tick: ', amount_per_tick);
  clearInterval(intId);
 } else {
  value = (value - amount_per_tick).toFixed(5);
  console.log('running', ticks_made, value);
 }

 ticks_made++;

}, interval);
```