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));
}
});
});
}
}
解决方案
假设你想用逗号分隔值,数百,千,百万,......
你可以这样做:
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));
推荐阅读
- php - 基于 Mysqli 查询的 PHP 重定向
- sql - 临时数据库中需要多少个临时数据库数据文件,微软是否有相同的建议?
- oauth - 以编程方式访问 G Suite 域公共文档(例如,G Drive 文档对所有员工公开)
- asp.net-mvc - 添加 Autofac Webapi2 后应用程序停止工作。没有例外
- snowflake-cloud-data-platform - SQL UDF 中传递的参数数量限制是多少?
- angular - valueChanges 在第二次模糊后有效,但在第一次模糊后无效
- c++ - 尽管没有外部库,但未解决的外部
- google-cloud-platform - 使用 gcloud 创建 Stackdriver 警报政策?(不从文件加载)
- git - powershell调用&运算符:设置输出编码
- python-3.x - `TypeError: *: 'Operation' 和 'int' 的不支持的操作数类型`。Keras张量流