JavaScript Round To 2 Decimal Places
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Dikshita
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> <html> <head> <title>TWO DECIMAL POINT</title> </head> <body> <div id="res"></div> <script> var num=3726.748375; num=Math.round((num+Number.EPSILON)*100)/100; document.getElementById('res').innerHTML=num; </script> </body> </html>
- <!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.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- In html we created <div> tag with ID ‘res’ for appends the result on webpage.
- In <script> we defined variable namely ‘num’ with ‘var’ keyword and initialized value is ‘3726.748375’.
- 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.
- 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.
- 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.
- 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.