1.

Why do we use a plus sign in front of function name in JavaScript?

Answer»

To round a number to specified decimal place in JavaScript, the toFixed() method is to be used.

Let us first SEE the syntax;

number.toFixed( [digits])

Here, digits is the decimal places to be set. For example, for 1 decimal place:

variable.toFixed(1))

The following is an example to round a number to 2 decimal places in JavaScript:

&LT;html&GT;    <head>       <title>JavaScript toFixed()</title>    </head>    <BODY>       <script>          VAR a = 291.78687;          document.write("Number = " + a);          document.write("<br>Result (2 decimal places) = " + a.toFixed(2));       </script>    </body> </html>

The output: 

Number = 291.78687 Result (2 decimal places) = 291.79

Let us see another example wherein we will round a number to 3 decimal places in JavaScript: 

<html>    <head>       <title>JavaScript toFixed()</title>    </head>    <body>       <script>          var a = 291.78687;          document.write("Number = " + a);          document.write("<br>Result (3 decimal places) = " + a.toFixed(3));       </script>    </body> </html>

The output: 

Number = 291.78687 Result (3 decimal places) = 291.787


Discussion

No Comment Found