Add a thousands separator to a total with Javascript or jQuery?

前端 未结 12 1869
灰色年华
灰色年华 2020-12-02 16:48

I have a function that sums a column of data in an html table. It does so admirably only I would like to have it put the commas in there that are needed to separate the thou

12条回答
  •  情歌与酒
    2020-12-02 17:22

    I know this is an uber old post and has good answers, BUT if anyone is interested, there is a jQuery plugin which simplifies number formatting (thousands formatting, number of decimal places, custom thousands separator, etc) by making an include and a simple call. Has lots of features and documentation is enough. It's called jQuery Number.

    You just include it:

    
    

    And then use it:

    On an automatic formatting HTML input: $('input.number').number( true, 2 );

    or

    On a JS call: $.number( 5020.2364, 2 ); // Outputs: 5,020.24

    Hopefully this helps someone.

提交回复
热议问题