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

JavascriptWeb DevelopmentFront End Technology

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 −

Live Demo

<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>
raja
Published on 18-Jan-2018 16:19:34
Advertisements