Computer >> Computer tutorials >  >> Programming >> Javascript

How can I round a number to 1 decimal place in JavaScript?


To round a number to 1 decimal place in JavaScript, use the toFixed() method.

The following is the syntax, wherein digits are the number of digits to appear after the decimal poin −

number.toFixed( [digits] )

Example

You can try to run the following code to round a number to 1 decimal places in JavaScript −

<html>
   <head>
      <title>JavaScript toFixed() Method</title>
   </head>
   
   <body>
      <script>
         var num = 488.234;
         document.write("num.toFixed(1) is : " + num.toFixed(1));
         document.write("<br />");
      </script>
   </body>
</html>