js

[JavaScript] ์ˆซ์ž ์ฒœ๋‹จ์œ„ ๋งˆ๋‹ค ์ฝค๋งˆ(,) ์ •๊ทœ์‹

natrue 2020. 9. 21. 14:21
728x90

์ •์ˆ˜ํ˜•์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ฌด๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋‚˜,

์†Œ์ˆ˜์ ์ด ์žˆ๋Š” ์‹ค์ˆ˜ํ˜•์˜ ๊ฒฝ์šฐ์—๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

 

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

 

 

1. ํ•จ์ˆ˜ 

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

 

 

 

2. td ๊ฐ’์— ์ฝค๋งˆ๋ฅผ ์ฐ์„ ๊ฒฝ์šฐ 

jquery

$("#id๋ช…").text().toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

 

javascript 

document.getElementById('id๋ช…').innerText.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");