How do I display large numbers with commas? HTML

后端 未结 3 1938
耶瑟儿~
耶瑟儿~ 2020-12-29 07:18

I am wanting to display large numbers more nicely with commas. So if the number was say 123456789, it would display 123,456,789. I have looked around but I only found code t

相关标签:
3条回答
  • 2020-12-29 07:44

    There's a simpler syntax for toLocaleString:

    Number(x).toLocaleString();
    

    This lets you drop the number in as a variable instead of converting the number into an object.

    0 讨论(0)
  • 2020-12-29 07:53

    This was answered here:

    How to print a number with commas as thousands separators in JavaScript

    In case you're not interested in reading the answer above, the code given was this:

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

    If you're using jquery use something like:

    var val = parseInt($('#value').text());
    //Use the code in the answer above to replace the commas.
    val = numberWithCommas(val);
    $('#value').text(val);
    
    0 讨论(0)
  • 2020-12-29 08:00

    You can use toLocaleString:

    num.toLocaleString('en', {useGrouping:true})
    
    0 讨论(0)
提交回复
热议问题