JS method: convert number to thousandth character

 

1  /* *
 2  * Convert the number to thousandth character
 3  * @param {Number} num
 4  * @param {Number} point retain several decimal places, default 2 digits
 5   */ 
6  function parseToThousandth(num, point = 2 ) {
 7    let [sInt, sFloat] = (Number.isInteger(num) ? `${num}` : num.toFixed(point)).split('.' );
 8    sInt = sInt.replace(/\d( ?=(\d{3})+$)/g, '$&,' );
 9    return sFloat ? `${sInt}.${sFloat}` : `${sInt}`;
 10  }
 11  
12 parseToThousandth (1234567); // '1,234,567'

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324688640&siteId=291194637