Round off a number upto 2 decimal place using JavaScript
A number can be rounded off to 2 decimal places using 2 approaches:
Method 1: Using the toFixed() method:
The toFixed() method is used to return a string representation of a number up to a certain number of digits after the decimal point. The number is rounded and the fractional part is padded if necessary to make it the specified length. It takes one parameter, that is the number of digits. This is an integer value between 0 to 20.
The toFixed() method is used with a value of 2 to round off a number upto 2 decimal places.
Syntax:
rounded_number = number.toFixed(2)
Example:
<!DOCTYPE html> <html> <head> <title> Round off a number upto 2 decimal place using JavaScript </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>Round off a number upto 2 decimal place using JavaScript </b> <p>Original floating point: 3.14159265359</p> <p> Rounding off to 2 decimal places: <span class="output"> </span> </p> <button onclick="roundOff()"> Round off to 2 places </button> <script type="text/javascript"> function roundOff() { pi = 3.14159265359; twoPlaces = pi.toFixed(2); document.querySelector( '.output').textContent = twoPlaces; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Method 2: Using the Math.round() method:
The Math.round() method is used to round a number its nearest integer value. It takes one parameter which is the number to be rounded.
The number is first multiplied by 100 to shift the decimal place so that it does not lose its value during the rounding off. The Math.round() method is applied to this number and the final result is divided by 100. This moves the decimal point back to its place as before. This number is the final rounded off number.
Syntax:
rounded_number = Math.round(number * 100) / 100
Example:
<!DOCTYPE html> <html> <head> <title> Round off a number upto 2 decimal place using JavaScript </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>Round off a number upto 2 decimal place using JavaScript </b> <p> Original floating point: 3.14159265359 </p> <p>Rounding off to 2 decimal places: <span class="output"> </span> </p> <button onclick="roundOff()"> Round off to 2 places </button> <script type="text/javascript"> function roundOff() { pi = 3.14159265359; twoPlaces = Math.round(pi * 100) / 100; document.querySelector( '.output').textContent = twoPlaces; } </script> </body> </html> |
Output:
- Before clicking the button:
- After clicking the button:
Recommended Posts:
- How to get decimal portion of a number using JavaScript ?
- JavaScript | Reverse a string in place.
- JavaScript | Math.round( ) function
- JavaScript | Math.round( ) function
- How to convert decimal to hex in JavaScript ?
- JavaScript | Number.MAX_VALUE & Number.MIN_VALUE with Examples
- How to convert a float number to the whole number in JavaScript?
- How to place button in top Right corner using bootstrap?
- How to place text in an image at any desired position ?
- How to place the image above the slider in mobile view in bootstrap?
- JavaScript | Number isSafeInteger()
- Javascript | Number() Function
- Number.isNaN() In JavaScript
- JavaScript | Number.isInteger( ) function
- Check a number is Prime or not using JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



