채택완료

숫자 3자리수 자동콤마

2020-08-26 (수) 13:28:10 4,319

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

아래 소스코드내에서 

opt_price / stotal

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

구글링을 해봐도

opt_price 는 적용이 안되고 

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

부탁좀 드리겠습니다.

Copy
<!-- 반복 -->
            <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>
|

답변 2개

채택된 답변
+20 포인트
2020-08-26 (수) 13:48:29

+ 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

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

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

    :
    $(this).closest('tr').find('.calc_price').text(new Intl.NumberFormat().format(quantity*price));

number_format(변수명)

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