'Javascript counter with decimal places
I have a jsfiddle here
It's a simple javascript function that counts up to a set number.
Is it possible to do this counting but with one decimal place as well
So it count 1.1, 1.2, 1.3 etc.
function startCounter(){
$('.counter').each(function (index) {
$(this).prop('Counter',0).animate({
Counter: $(this).text()
}, {
duration: 2000,
easing: 'swing',
step: function (now) {
$(this).text(Math.ceil(now));
}
});
});
}
startCounter();
Solution 1:[1]
This is what I did: Replace $(this).text(Math.ceil(now)) with $(this).text(Math.ceil(now)/10) and Counter: $(this).text() by Counter: $(this).text()*10. It will increment by tenths. For each additional decimal place divide or multiply by 10.
Solution 2:[2]
change one line..
$(this).text(Math.ceil(now));
to
$(this).text(Math.round( now * 10 ) / 10);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SalmonKiller |
| Solution 2 |
