我有一个数字计数器,从0到指定值。目前它适用于固定小数点,但如果值超过1,000,则不会添加逗号(例如,如果我输入10000,我应该预期10,000,但它当前返回10000.00)。
有人可以帮我解决这个问题吗?我提供了完整的功能,但是运行动画的唯一部分在// ANIMATION STARTS HERE开头的评论中有详细说明。
提前谢谢!
numbersAnimate(_Panel) {
if (!_Panel) {
return;
}
const _Document = document.documentElement;
let lang = _Document.getAttribute('lang');
const _PanelAmounts = _Panel.querySelectorAll('.panel__amount');
if (!_PanelAmounts) {
return;
}
for (const _PanelAmount of _PanelAmounts) {
const $PanelAmount = $(_PanelAmount);
const value = $PanelAmount.data('amount');
if (!value || value.length < 1) {
return;
}
if (isNaN(value)) {
return;
}
//ANIMATION STARTS HERE....
$PanelAmount.prop('Counter', 0).animate({
Counter: value
}, {
duration: 1000,
easing: 'linear',
step: function(now) {
$PanelAmount.text(this.Counter.toFixed(2));
}
});
}
_Panel.classList.add('animated');
//JSON Structure
"statistic": {
"amount": "10000",
"symbol": "%"
},
<span className="panel__amount panel-text-style-c" data-amount={jsonData.statistic.amount}>0</span>
答案 0 :(得分:1)
您可以使用toLocaleString()
var number = 1547885415.235489654
console.log('Correctly formated number : ', number.toLocaleString('en-US', {maximumFractionDigits: 2}));