All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Round To 2 Decimal Places

Last Updated : Mar 11, 2024

JavaScript Round To 2 Decimal Places

In this tutorial we will show you the solution of JavaScript round to 2 decimal places, here we defined decimal number with some amount of floating points we have to round those floating points and allow up to two digits only after point for that we used Math object.

Math object is a built-in object that provides properties and methods for mathematical constants functions to execute mathematical operations.

So we used Math.round() function to returns the value of a number rounded to the nearest integer.

Step By Step Guide On JavaScript Round To 2 Decimal Places :-

Here we using round() method in Math object for gets the result. We defined random decimal number with floating points and stored to variable ‘num’ then using Number property EPSILON with 100 multiplication so we get two digits after floating point as a whole number.

The Math.round() method is rounds the last digit with nearest digit then we divided by 100 for sets floating point as two.

Finally we gets result so we appends result with div element for displaying result in webpage.

<!DOCTYPE html>
        <title>TWO DECIMAL POINT</title>
        <div id="res"></div>
            var num=3726.748375;
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In html we created <div> tag with ID ‘res’ for appends the result on webpage.
  7. In <script> we defined variable namely ‘num’ with ‘var’ keyword and initialized value is ‘3726.748375’.
  8. The Number with EPSILON property takes two digits after floating point to the left hand side and remaining digits presents after floating point by multiplication of 100.
  9. Using Math.round() method after floating point presented digits removed and before floating point digit rounded as nearest digit then we gets floating point at before last two digits by dividing with 100.
  10. Then the result is stored to variable ‘num’ and appends to html div element by ‘innerHTML’. So we appended result with div tag will displayed on webpage.
  11. Both </body>, </html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to round to two decimal places using javascript.

When we executing this program on browser we can see the result of floating with two digits number with concept heading displayed on webpage.

We can use toFixed() method also for getting same result but when we using round method we will get accurate round calculation result and we can set any number of floating digits by using this concept later we will discuss about this.

I hope this tutorial on JavaScript round to 2 decimal places helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪