
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to format a rounded number to N decimals using JavaScript?
Use the toFixed() method to format a rounded number to N decimals. The toFixed() method formats a number with a specific number of digits to the right of the decimal.
Example
You can try to run the following code to form a rounded number to N decimals −
<html> <head> <title>JavaScript toFixed() Method</title> </head> <body> <script> var num = 15; document.write("num.toFixed() is : " + num.toFixed()); document.write("<br />"); document.write("num.toFixed() is : " + num.toFixed(2)); document.write("<br />"); document.write("num.toFixed() is : " + num.toFixed(1)); document.write("<br />"); </script> </body> </html>
Output
num.toFixed() is : 15 num.toFixed() is : 15.00 num.toFixed() is : 15.0
- Related Articles
- How to format a number with two decimals in JavaScript?
- How to format hexadecimal Number in JavaScript?
- How to get the value of a number rounded to the nearest integer in JavaScript?
- Return each element of the masked array rounded to the nearest given number of decimals in NumPy
- How to convert a String containing Scientific Notation to correct JavaScript number format?
- How to format a JavaScript date?
- How to draw rounded line ends using Matplotlib?
- How to format a float in JavaScript?
- How to add rounded corner to an element using CSS?
- How to round down to 2 decimals a float using Python?
- How to create a Number object using JavaScript?
- How to convert a decimal number to roman using JavaScript?
- How to format number in JSP?
- How to format JavaScript date into yyyy-mm-dd format?
- How to check if a number evaluates to Infinity using JavaScript?

Advertisements