繁体   English   中英

如何在JS中将数字格式更改为小数

[英]How can I change number format to decimals in JS

我有这个计数器,我需要它是小数(而不是 150000000,我想要 150.000.000,00 或至少 150.000.000)。

我试过替换这个:

step: function() {
    $this.text(Math.floor(this.countNum));
}

有了这个:

formatter: function (value, options) {
    return value.toFixed(options.decimals);
}

但它没有用。 我能做些什么?

这些是我感兴趣的 js 和 html 的一部分:

 var a = 0; $(window).scroll(function() { var oTop = $('#counter').offset().top - window.outerHeight/2; if (a == 0 && $(window).scrollTop() > oTop) { euro.className = "testo one column hide" $('.counter-value').each(function() { var $this = $(this), countTo = $this.attr('data-count'); $({ countNum: $this.text() }).animate({ countNum: countTo }, { duration: 2000, easing: 'swing', step: function() { $this.text(Math.floor(this.countNum)); }, complete: function() { $this.text(this.countNum); //alert('finished'); } }); }); a = 1; } else { euro.className = "testo one column show" } });
 <div id="counter" class="two columns counter-value" data-count="150000000"> </div>

toFixed 是一个很好的方法,但它只会处理最后的小数。 要完成您所描述的内容,您可能需要使用 number.toLocaleString()。 所以你的格式化程序看起来像:

formatter: function(value, options) {
     return value.toLocaleString(options);
}

您可以在下面的 MDN 文档中查看 toLocaleString 选项:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

希望有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM