Javascript 试图在计数器中用逗号分隔数字

Javascript 试图在计数器中用逗号分隔数字,javascript,Javascript,我试图输出最终的数值,但格式为货币,以显示货币符号和逗号。我尝试集成我在这里找到的转换函数,但在将其包含在整个代码中时遇到了一些问题。我对这一点很陌生,所以请让我知道我在你的评论中哪里出错了,最好的方向是什么 这是计数器的代码: 带逗号的函数编号(x){ var parts=x.toString().split(“.”); 零件[0]=零件[0]。替换(/\B(?=(\d{3})+(?!\d))/g,“,”; 返回零件。连接(“.”); } var a=0; $(窗口)。滚动(函数(){ va

我试图输出最终的数值,但格式为货币,以显示货币符号和逗号。我尝试集成我在这里找到的转换函数,但在将其包含在整个代码中时遇到了一些问题。我对这一点很陌生,所以请让我知道我在你的评论中哪里出错了,最好的方向是什么

这是计数器的代码:

带逗号的函数编号(x){
var parts=x.toString().split(“.”);
零件[0]=零件[0]。替换(/\B(?=(\d{3})+(?!\d))/g,“,”;
返回零件。连接(“.”);
}
var a=0;
$(窗口)。滚动(函数(){
var oTop=$('#counter').offset().top-window.innerHeight;
if(a==0&&$(window.scrollTop()>oTop){
$('.counter value')。每个(函数(){
变量$this=$(this),
countTo=$this.attr('data-count');
$({
countNum:numberWithCommas.call($this.text())
}).制作动画({
countNum:countTo
}, {
期限:2000年,
放松:"摇摆",,
步骤:函数(){
$this.text(Math.floor(this.countNum));
},
完成:函数(){
$this.text(this.countNum);
}
});
});
a=1;
}
});
。计数器值{
字体大小:60px;
线高:2米;
文本对齐:居中;
填充:17px0;
}
.计数器值:在{
内容:attr(数据描述);
显示:块;
文本转换:大写;
字体大小:14px;
线高:1.2米;
}

0
0

正如卡瓦尔吉特·辛格所建议的,或者简单地使用toLocaleString()

const nForm=new Intl.NumberFormat('en-en')
,nFormUSD=new Intl.NumberFormat('en-en',{style:“currency”,currency:“USD”})
,nFormFix2=v=>nForm.format(v.toFixed(2))
;
设ValueX=1234567.123456;
console.log('Intl.NumberFormat=>',nForm.format(ValueX))
console.log('Intl.NumberFormat USD=>',nFormUSD.format(ValueX))
console.log('Intl.NumberFormat in nFormFix2',nFormFix2(ValueX))
console.log(“x.tolocalString('en-en')”,ValueX.tolocalString('en-en'))

.as console wrapper{max height:100%!important;top:0;}
如Kawaljit Singh所建议,或者简单地使用toLocaleString()

const nForm=new Intl.NumberFormat('en-en')
,nFormUSD=new Intl.NumberFormat('en-en',{style:“currency”,currency:“USD”})
,nFormFix2=v=>nForm.format(v.toFixed(2))
;
设ValueX=1234567.123456;
console.log('Intl.NumberFormat=>',nForm.format(ValueX))
console.log('Intl.NumberFormat USD=>',nFormUSD.format(ValueX))
console.log('Intl.NumberFormat in nFormFix2',nFormFix2(ValueX))
console.log(“x.tolocalString('en-en')”,ValueX.tolocalString('en-en'))

.as控制台包装{最大高度:100%!重要;顶部:0;}
请为我们提供一些输入和输出示例,以便为您提供适当的答案。我刚刚读到,有一种叫做Intl.NumberFormat的东西可以更改数字格式。请为我们提供一些输入和输出示例,以便为您提供适当的答案。我刚刚读到,有一种叫做Intl.NumberFormat的东西可以更改数字格式。