Front End/🌈 JavaScript
천단위 표기 및 컴마 제거
James Wetzel
2024. 4. 15. 14:10
천단위 별로 컴마(comma)를 추가한다.
return type = string
String.prototype.setComma = function () {
return isNaN(Number(this)) ? "0" : Number(this).toLocaleString("ko-KR");
}
console.log("100000".setComma());
// > "100,000"
Number.prototype.setComma = function () {
return isNaN(Number(this)) ? "0" : Number(this).toLocaleString("ko-KR");
}
console.log((100000000).setComma());
// > "100,000,000"
컴마(comma)를 제거한다.
return type = number
String.prototype.removeComma = function(){
return isNaN(Number(this.replace(/,/g, ''))) ? 0 : Number(this.replace(/,/g, ''));
}
console.log("10,000".removeComma())
// > 10000
728x90
반응형