JavaScript Format Numbers With Commas Example

Nov 10, 2021 . Admin



Hello Friends,

Now let's see example of how to check format numbers with commas example. We will use how to check format numbers with commas in javascript. Here you will learn how to check format numbers with commas. Let's get started with how to check format numbers with commas in javascript.

Here i will give you many example how to check format numbers with commas javascript.

Example : 1
<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript Format Numbers With Commas Example - MyWebtuts.com</title>
    </head>
    <body>
        <h1> Welcome to the MyWebtuts.com </h1>
        <p>This is an example of using the JavaScript <b> toLocaleString() </b> method to format  a number with commas as thousands of	separators in JavaScript</p>

        <b> Num1 = '123456789' </b> </br>
        <b> Num2 = '1234.56789' </b>
        <p> Click the below button to print the above numbers separated with commas </p>
        <button onclick = "test()"> Click me </button>

        <script type = "text/javascript">
            function test() {
                a1 = 123456789;
                a2 = 1234.56789;
                opt1 = a1.toLocaleString('en-US');
                opt2 = a2.toLocaleString('en-US');
                document.write("<b> Given number = </b>", a1);
                document.write("</br> <b> Formatted number = </b>", opt1);

                document.write("</br> <br> <b> Given number = </b>", a2);
                document.write("</br> <b> Formatted number = </b>", opt2);
            }
        </script>
    </body>
</html>
Output :
Given number = 123456789
Formatted number = 123,456,789

Given number = 1234.56789
Formatted number = 1,234.568
Example : 2
<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript Format Numbers With Commas Example - MyWebtuts.com</title>
    </head>
    <body>
        <h1> Welcome to the MyWebtuts.com </h1>
        <p>This is an example of using the <b> Intl.NumberFormat() </b> object to format a number with commas as thousands of separators in JavaScript</p>

        <b> Num1 = '649850128' </b> </br>
        <b> Num2 = '6498.50128' </b>
        <p> Click the below button to print the above numbers separated with commas </p>
        <button onclick = "test()"> Click me </button>
        
        <script type = "text/javascript">
            function test() {
                a1 = 649850128;
                obj1 = new Intl.NumberFormat('en-US');
                opt1 = obj1.format(a1);
                document.write("<b> Given number = </b>", a1);
                document.write("</br> <b> Formatted number = </b>", opt1);

                a2 = 6498.50128;
                obj2 = new Intl.NumberFormat('en-US');
                opt2 = obj2.format(a2);
                document.write("</br> <br> <b> Given number = </b>", a2);
                document.write("</br> <b> Formatted number = </b>", opt2);
            }
        </script>
    </body>
</html>
Output :
Given number = 482351769
Formatted number = 482,351,769

Given number = 4823.51769
Formatted number = 4,823.518

It will help you...

#Javascript