javascript - 数字脚本之间的逗号

标签 javascript

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

谢谢。

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));
                }
            });
        });
    }
}

最佳答案

假设您想要用逗号分隔值,由 hunders,thousands,millions,...

你可以这样做:

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));

关于javascript - 数字脚本之间的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55072574/

相关文章:

javascript - 如何将动态变量从 javascript 分配给 angularJS 范围

php - 为什么我的 AJAX GET 请求需要 2-3 秒,而相同的 MYSQL 命令行查询只需要不到 0.1 秒?

javascript - 多个 document.getElementById

javascript - 在 React Native Android 和 iOS 中使用 Set Interval

javascript - 如何处理传递/返回数组指针到 emscripten 编译代码?

javascript - 回调处理

javascript - $window.open 仍在phonegap应用程序中打开url( Angular )

javascript - 用户输入的 Reddit 搜索 API 查询

javascript - 使用函数更改值时,innerHTML 不会立即工作

javascript - 以编程方式创建的类的 CSS 样式