목차

JavaScript 원화 표시

  • description : JavaScript 원화 표시
  • author : 오션
  • email : shlim@repia.com
  • lastupdate : 2023-01-26 Thu


JavaScript 원화 표시

const price = "14340";
let USDollar = new Intl.NumberFormat('en-Us', {
    style: 'currency',
    currency: 'USD'
});
console.log(`The foramtted version of ${price} is ${USDollar.format(price)}`);
// The foramtted version of 14340 is $14,340.00
 
 
const price1 = "3722407";
let result = new Intl.NumberFormat('ko-KR').format(price1);
console.log(result);
// 3,722,407
 
 
const price2 = 1270000;
let KRWon = new Intl.NumberFormat('ko-KR', {
    style: 'currency',
    currency: 'KRW'
});
 
let KRWonChanged = KRWon.format(price2);
 
console.log(typeof price2); // number
console.log(KRWonChanged);  // ₩1,270,000
console.log(typeof KRWonChanged);   // string
 
let original = KRWonChanged.substring(1).replace(/\,/g,'');
console.log(original);  // 1270000


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <title>Regex Test</title>
</head>
<body>
 
    <div>
        <label for="test">regexTest</label>
        <input id="test" type="text">
    </div>
 
 
    <script>
        $(document).on('keyup', 'input#test', function(){
 
            $(this).val($(this).val().replace(/[^0-9]/,''));
            let $inputVal = $(this).val();
 
            console.log($inputVal);
 
            let KRWon = new Intl.NumberFormat('ko-KR', {
                style: 'currency'
                , currency : 'KRW'
            });
 
            let changedToKRWon = KRWon.format($inputVal);
            console.log(changedToKRWon);
 
            $inputVal = changedToKRWon;
 
            $('input#test').on('focusout', function(){
                $(this).val(changedToKRWon);
            });
 
            $('input#test').on('focusin', function(){
                let original = changedToKRWon.substring(1).replace(/\,/g,'');
                $(this).val(original);
            })
        });
 
    </script>
</body>
</html>


Intl.NumberFormat
ISO 4217 통화코드
Currency Symbol 통화 기호