首页 > 解决方案 > 数字脚本之间的逗号

问题描述

我已经尝试了无数解决方案,这些解决方案在这些论坛上找到了,但我没有找到工作,或者我只是把它放在错误的地方。我正在尝试为成千上万的地方强制使用逗号。任何建议和安置将不胜感激。

谢谢你。

jQuery(window).scroll(startCounter);

function startCounter() {
    var hT = jQuery('.counter').offset().top,
        hH = jQuery('.counter').outerHeight(),
        wH = jQuery(window).height();
    if (jQuery(window).scrollTop() > hT+hH-wH) {
        jQuery(window).off("scroll", startCounter);
        jQuery('.counter').each(function () {
            var $this = jQuery(this);
            jQuery({ Counter: 0 }).animate({ Counter: $this.text() }, {
                duration: 4000,
                easing: 'swing',
                step: function () {
                    $this.text(Math.ceil(this.Counter));
                }
            });
        });
    }
}

标签: javascript

解决方案


假设你想用逗号分隔值,数百,千,百万,......

你可以这样做:

let num = 9876543210;

console.log(num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
// or
console.log((num).toLocaleString());
// or
console.log(new Intl.NumberFormat('en-US', {}).format(num));


推荐阅读