숫자 3자리수 자동콤마
본문
안녕하세요 질문좀 드리겠습니다.
아래 소스코드내에서
opt_price / stotal
위두개가 숫자로만 이루어져 있는데 글을쓸때도 자동으로 3자릿수 콤마 되게끔 하는방법이있을까요?
구글링을 해봐도
opt_price 는 적용이 안되고
stotal 는 콤마가 풀리는 현상이 나오더라구요 ㅜ
부탁좀 드리겠습니다.
<!-- 반복 -->
<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
+ 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
짧은 코드니까, 어떤 명령이 어떤 역할을 하는지 이해 후에 응용&활용하면 되겠습니다.
<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(변수명)
답변을 작성하시기 전에 로그인 해주세요.