πŸ’»Coding/πŸ“˜ JavaScript

JavaScript 숫자 콀마 : 3μžλ¦¬λ§ˆλ‹€ 콀마 ν‘œμ‹œν—ˆ | μžλ°”μŠ€ν¬λ¦½νŠΈ JS

μŒμ€μ‘ 2023. 3. 18. 08:00
728x90
λ°˜μ‘ν˜•
JavaScript 
숫자 3μžλ¦¬λ§ˆλ‹€ 콀마 ν‘œμ‹œν•˜κΈ°
μžλ°”μŠ€ν¬λ¦½νŠΈ JS
/
" 숫자λ₯Ό ν‘œμ‹œν• λ•Œ 3μžλ¦¬λ§ˆλ‹€ μ½€λ§ˆκ°€
ν‘œμ‹œλ˜μ—ˆμœΌλ©΄ 쒋겠을 λ•Œ μ‚¬μš©ν•˜λŠ” μ½”λ“œ.✌️
κ°€μž₯ μ‰¬μš΄ 방법은 toLocaleString()을 μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.😊"
/

β¬‡οΈβ¬‡οΈβ¬‡οΈμˆ«μž 콀마 κ΅¬ν˜„β¬‡οΈβ¬‡οΈβ¬‡οΈ


 

<!DOCTYPE html>
<html lang="ko">

<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">
    <title>toLocaleString()</title>
    <!-- font -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap" rel="stylesheet">
    <!-- css -->
    <style>
        @import url("https://cdn.jsdelivr.net/gh/orioncactus/pretendard/dist/web/static/pretendard.css");
        @import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap');

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            outline: none;
            word-break: break-all;
            font-family: 'Montserrat', "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
        }

        input {
            width: 40rem;
            height: 3.5rem;
            border-radius: 5px;
            padding-left: 1rem;
            border: 2px solid #009b2e;
            background: #f2f2f2;
        }

        h2 {
            color: #009b2e;
            margin: .6rem 0;
        }

        .box>div {
            margin: 2rem 1rem;
        }
    </style>
</head>

<body>
    <div class="box">
        <div>
            <h2>&#8675; 숫자λ₯Ό μž…λ ₯ν•˜λ©΄ μžλ™μœΌλ‘œ 3μžλ¦¬λ§ˆλ‹€ μ½€λ§ˆκ°€ ν‘œμ‹œλ©λ‹ˆλ‹€ &#8675;</h2>
            <input type="text" oninput="formatPrice(this)">
        </div>
        <div></div>
</body>
<script>
    const formatPrice = (target) => {
        // 숫자만 남긴 ν›„, 포맷
        target.value = Number(target.value
            .replace(/[^0-9]/g, ''))
            .toLocaleString();
    }
</script>

</html>

728x90
λ°˜μ‘ν˜•