테스트 사이트 - 개발 중인 베타 버전입니다

숫자 3자리수 자동콤마 채택완료

초비입니다 5년 전 조회 4,157

안녕하세요 질문좀 드리겠습니다.

 

아래 소스코드내에서 

 

opt_price / stotal

 

위두개가 숫자로만 이루어져 있는데 글을쓸때도 자동으로 3자릿수 콤마 되게끔 하는방법이있을까요?

구글링을 해봐도

 

opt_price 는 적용이 안되고 

stotal 는 콤마가 풀리는 현상이 나오더라구요 ㅜ

 

부탁좀 드리겠습니다.

 

</p>

<p><!-- 반복 -->

            <tr>

                <td class="td_num">

                    <label for="ct_qty_<?php echo $chk_cnt; ?>" class="sound_only" value="<?php echo $opt['ct_qty']; ?>"> 수량</label>

                    <span class="quantity"><?php echo $opt['ct_qty']; ?></span>

                </td>

                <td class="td_num_right "><input type="text" name="opt_price" class="price" value="<?php echo $opt_price ?>"> </td>

                <td class="td_num_right"><span class="calc_price"><?php echo number_format($ct_price['stotal']); ?></span></td>

            </tr>

<!-- /반복 -->



<script>

// 반복되는 부분 말고, 한번만 출력되면 OKAY

$('.price').on('keyup', function() {

    var quantity = parseInt($(this).closest('tr').find('.quantity').text().replace(/[^\d]+/g, '')) | 0;

    var price = parseInt($(this).val().replace(/[^\d]+/g, '')) | 0;

    $(this).closest('tr').find('.calc_price').text(quantity*price);

});

</script></p>

<p>

댓글을 작성하려면 로그인이 필요합니다.

답변 2개

채택된 답변
+20 포인트
B
5년 전

+ Intl.NumberFormat

- [영] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat

- [한] https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat

짧은 코드니까, 어떤 명령이 어떤 역할을 하는지 이해 후에 응용&활용하면 되겠습니다.

</p>

<p>    <td class="td_num_right "><input type="text" name="opt_price" class="price" value="<?php echo number_format($opt_price); ?>"> </td></p>

<p>    :

    $(this).closest('tr').find('.calc_price').text(new Intl.NumberFormat().format(quantity*price));</p>

<p>

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

cho개발
5년 전

number_format(변수명)

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인