# amtFormatJson 함수 기본 구조
function amtFormatJson(amount) {
if (!amount) return "";
amount = String(amount).replace(/[^0-9]/g, "");
return amount.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
# 사용 예시
// 일반 숫자 포맷팅
amtFormatJson(10000); // "10,000"
amtFormatJson("5000000"); // "5,000,000"
// jQuery에서 활용
$("#price").text(amtFormatJson(data.price));
// form 데이터 포맷팅
$("input[name='amount']").val(amtFormatJson($("#amount").val()));
# 실제 활용 케이스
// 표시할 때
$('#totalAmount').text(amtFormatJson(price) + "원");
// 서버 전송 전 콤마 제거
function removeComma(str) {
return str.replace(/,/g, "");
}
$('form').submit(function() {
var price = removeComma($('#price').val());
$('#price').val(price);
});
# 주의 사항
- 숫자가 아닌 문자는 모두 제거됨
- null, undefined 입력 시 빈 문자열 반환
- 소수점 처리 불가 (정수만 처리)