JavaScript - toFixed() method

revision:


Category : number

The toFixed() method formats a number using fixed-point notation.

Syntax :

        toFixed()
        toFixed(digits)
    

Parameters:

digits : optional. The number of digits to appear after the decimal point; should be a value between 0 and 100, inclusive. If this argument is omitted, it is treated as 0.

Examples:

        function financial(x) {
            return Number.parseFloat(x).toFixed(2);
        }
        console.log(financial(123.456));
          // Expected output: "123.46"
        console.log(financial(0.004));
          // Expected output: "0.00"
        console.log(financial('1.23e+5'));
          // Expected output: "123000.00"
          
        (1000000000000000128).toString(); // '1000000000000000100'
        (1000000000000000128).toFixed(0); // '1000000000000000128'
        (0.3).toFixed(50); // '0.29999999999999998889776975374843459576368331909180'

    

Practical examples

example: using the toFixed() method.

code:
                    <div>
                        <p id="fixed-1"></p>
                        <p id="fixed-2"></p>
                        <p id="fixed-3"></p>
                        <p id="fixed-4"></p>
                        <p id="fixed-5"></p>
                        <p id="fixed-6"></p>
                        <p id="fixed-7"></p>
                        <p id="fixed-8"></p>
                        <p id="fixed-9"></p>
                        <p id="fixed-10"></p>
                        <p id="fixed-11"></p>
                    </div>
                    <script>
                        const numObj = 12345.6789;
                        document.getElementById("fixed-1").innerHTML = "number : " + numObj; 
                        document.getElementById("fixed-2").innerHTML = "number toFixed() : " + numObj.toFixed(); // '12346'; rounding, no fractional part
                        document.getElementById("fixed-3").innerHTML = "number toFixed(1) : " + numObj.toFixed(1); // '12345.7'; it rounds up
                        document.getElementById("fixed-4").innerHTML = "number toFixed(6) : " + numObj.toFixed(6); // '12345.678900'; additional zeros
                        document.getElementById("fixed-5").innerHTML = "number toFixed(2) : " + (1.23e20).toFixed(2); // '123000000000000000000.00'
                        document.getElementById("fixed-6").innerHTML = "number toFixed(2) : " + (1.23e-10).toFixed(2); // '0.00'
                        document.getElementById("fixed-7").innerHTML = "number toFixed(1) : " + (2.34).toFixed(1); // '2.3'
                        document.getElementById("fixed-8").innerHTML = "number toFixed(1) : " + (2.35).toFixed(1); // '2.4'; it rounds up
                        document.getElementById("fixed-9").innerHTML = "number toFixed(1) : " + (2.55).toFixed(1); // '2.5'
                        // it rounds down as it can't be represented exactly by a float and the
                        // closest representable float is lower
                        document.getElementById("fixed-10").innerHTML = "number toFixed(1) : " + (2.449999999999999999).toFixed(1); // '2.5'
                        // it rounds up as it's less than NUMBER.EPSILON away from 2.45.
                        // This literal actually encodes the same number value as 2.45
            
                        document.getElementById("fixed-11").innerHTML = "number toFixed(50) : " + (6.02 * 10 ** 23).toFixed(50); 
                        // 6.019999999999999e+23; large numbers still use exponential notation
                    </script>