계산 시 콤마 첨삭?

계산 시 콤마 첨삭?

QA

계산 시 콤마 첨삭?

본문

3695953000_1678239330.1414.jpg

 

상단 이미지와 같은 폼을 제작하고 있습니다.

 


$("#qty").change(function() {
price = $("#qty option:selected").val();
console.log(price);
sum = parseInt(price);
if(price) {
    $("#it_sum1").val(sum * 1000000);
    cal_sum();
}
});
var sum2 = 0;
$("#qty2").change(function() {
price2 = $("#qty2 option:selected").val();
console.log(price2);
sum2 = parseInt(price2);
if(price2) {
    $("#it_sum2").val(sum2 * 500000);
    cal_sum();
}
});
function cal_sum() {
    // $("#it_sum1").val().replace(/,/g,"");  
    var sum = parseInt($("#it_sum1").val()) + parseInt($("#it_sum2").val());
    $('#it_sum_all').val(sum);
    $('#it_sum_all').val($('#it_sum_all').val().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
    var vat = sum * 0.1;
    $('#it_sum_vat').val(vat);
    $('#it_sum_vat').val($('#it_sum_vat').val().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
    var total = sum + vat;
    $('#it_sum_total').val(total);
    $('#it_sum_total').val($('#it_sum_total').val().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
};

 

현재 스크립트는이렇게 되어있는데

#it_sum1 인풋에 콤마를 찍고싶습니다. 콤마를 넣는 정규식을 적용하였는데

cal_sum에서 합산될때 계산이 이상하게 되버리네요....

 

어떻게 해야 좋을까요?

이 질문에 댓글 쓰기 :

답변 3

표현할때 comma 추가

https://github.com/gnuboard/gnuboard5/blob/master/js/common.js#L64


function number_format(data)

 

계산할때 comma 제거

https://github.com/gnuboard/gnuboard5/blob/master/js/common.js#L119


function no_comma(data)

 


<select id="qty">
    <option value=""></option>
    <option value="1">1</option>
    <option value="2">2</option>
</select>
<select id="qty2">
    <option value=""></option>
    <option value="1">1</option>
    <option value="2">2</option>
</select>
<ul>
<li><label>it_sum1 <input type="text" id="it_sum1" /></label></li>
<li><label>it_sum2 <input type="text" id="it_sum2" /></label></li>
<li><label>it_sum_all <input type="text" id="it_sum_all" /></label></li>
<li><label>it_sum_vat <input type="text" id="it_sum_vat" /></label></li>
<li><label>it_sum_total <input type="text" id="it_sum_total" /></label></li>
</ul>
<script>
$("#qty").change(function() {
    price = $("#qty option:selected").val();
    if (price == '') {
        $("#it_sum1").val('');
        cal_result_init();
        return;
    }
    // console.log(price);
    // sum = parseInt(price);
    sum = Number(price);
    if(price) {
        // $("#it_sum1").val(sum * 1000000);
        $("#it_sum1").val(number_format(sum * 1000000));
        cal_sum();
    }
});
var sum2 = 0;
$("#qty2").change(function() {
    price2 = $("#qty2 option:selected").val();
    if (price2 == '') {
        $("#it_sum2").val('');
        cal_result_init();
        return;
    }
    // console.log(price2);
    // sum2 = parseInt(price2);
    sum2 = Number(price2);
    if(price2) {
        // $("#it_sum2").val(sum2 * 500000);
        $("#it_sum2").val(number_format(sum2 * 500000));
        cal_sum();
    }
});
function cal_sum() {
    // // $("#it_sum1").val().replace(/,/g,"");  
    // var sum = parseInt($("#it_sum1").val()) + parseInt($("#it_sum2").val());
    // $('#it_sum_all').val(sum);
    // $('#it_sum_all').val($('#it_sum_all').val().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
    // var vat = sum * 0.1;
    // $('#it_sum_vat').val(vat);
    // $('#it_sum_vat').val($('#it_sum_vat').val().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
    // var total = sum + vat;
    // $('#it_sum_total').val(total);
    // $('#it_sum_total').val($('#it_sum_total').val().replace(/\B(?=(\d{3})+(?!\d))/g, ","));
    var sum = Number(no_comma($("#it_sum1").val())) + Number(no_comma($("#it_sum2").val()));
    $('#it_sum_all').val(number_format(sum));
    var vat = sum * 0.1;
    $('#it_sum_vat').val(number_format(vat));
    var total = sum + vat;
    $('#it_sum_total').val(number_format(total));
};
function cal_result_init() {
    $('#it_sum_all').val('');
    $('#it_sum_vat').val('');
    $('#it_sum_total').val('');
}
</script>

기본적으로 스크립트상에서 ,가 포함된 숫자형을 쓰시려고 하시면 실제로는 문자로 인식되기때문에

숫자형으로 형변환후에 처리를 하셔야 할거구요

 

input안에 들어가는 문자형으로도 표현은 가능하지만 그전에 연산은 하신후에 넣으시길 추천합니다.

답변을 작성하시기 전에 로그인 해주세요.
전체 161
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT