Adding comma as thousands separator (javascript) - output being deleted instead

前端 未结 5 1015
你的背包
你的背包 2021-01-02 09:55

I am attempting to dynamically adjust a numerical value entered to include thousand separators

Here is my code:

function addCommas(nStr) {
    nStr +         


        
5条回答
  •  孤独总比滥情好
    2021-01-02 10:36

    In each case before formatting try to remove existing commas first, like there: Removing commas in 'live' input fields in jquery

    Example:

    function addThousandsSeparator(x) {
        //remove commas
        retVal = x ? parseFloat(x.replace(/,/g, '')) : 0;
    
        //apply formatting
        return retVal.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
    

提交回复
热议问题