【问题标题】:Adding thousands separator to variable with a period in its name in Javascript在Javascript中将千位分隔符添加到名称中带有句点的变量
【发布时间】:2017-06-14 14:53:56
【问题描述】:

我对 Javascript 不太熟悉,所以可能有一个非常简单的解决方案。我要做的就是在这个函数的输出中添加逗号千位分隔符:

function() {
return '$' + this.value;
}

因此,例如 $100,000 将显示为 $100,000。我的问题是,由于句号,我不知道如何处理变量 this.value。

我试过了:

function numberWithCommas(this.value) {
    return '$' + this.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

我的猜测是 this.value.toString() 的语法不正确。

提前致谢。

【问题讨论】:

标签: javascript separator


【解决方案1】:

希望对你有所帮助:

var n = 1123456789.123
n.toLocaleString()
"1,123,456,789.123"

查看更多:toLocaleString

  1. 您可以使用:Numeral.js - 非常强大

而且:在未来,你真的会一次又一次地研究和尝试。

谢谢

【讨论】:

    【解决方案2】:

    这行得通:

    function numberWithCommas() {
        return '$' + this.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2013-06-17
      • 2014-09-28
      • 1970-01-01
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多