Number numerica...LOGIN

Number numerical object in JavaScript

Number numerical object

A numerical variable is a numerical object (Number object).



##toFixed()

  • Function: Change a value Convert to a string and round, retaining the specified number of decimal places.

  • Syntax: numObj.toFixed(n)

  • Parameters: n is the number of decimal places to be retained.

  • Example:

var a = 123.9878;

a = a.toFixed(2); // a = “123.99”

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            var x=134.456756;
            document.write(x.toFixed(3));
        </script>
    </head>
    <body>
    </body>
</html>

Example: Find the area of ​​a circle

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            //求圆的面积
            function getArea(r){
                var s=Math.PI*r*r;
                s=s.toFixed(4);
                document.write("半径为:"+r+"的圆,面积为:"+s+"<br/>");
            }
            getArea(2);
            getArea(4);
            getArea(5);
        </script>
    </head>
    <body>
    </body>
</html>

##Next Section

<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var x=134.456756; document.write(x.toFixed(3)); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware