How to Format a Number with Two Decimals in JavaScript? Last Updated : 29 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In javascript, formatting numbers to a fixed number of decimal places is a common requirement, particularly in financial calculations, statistical data presentations, and to show in user interfaces. In this article we will explore different approaches to format a number with two decimals in JavaScript.Below are different approaches to format a number with two decimals in JavaScript:Table of ContentUsing the toFixed() methodUsing the Number.prototype.toPrecision() MethodUsing Mathematical Rounding with Math.round()Using the toFixed() methodIn this approach we are using the toFixed() method. This method formats a number using fixed-point notation. It converts the number to a string and keep a specified number of decimals.Syntax:number.toFixed(digits)Where:digits is the number of digits to appear after the decimal point.Example: In below example we are using the toFixed() method to format a number with two decimals. JavaScript let num = 123.4564; let formattedNum = num.toFixed(2); console.log(formattedNum); Output123.46 Using the Number.prototype.toPrecision() MethodIn this approach we are using the toPrecision() method. This method formats a number to a specified precision (total number of significant digits).Syntax:number.toPrecision(precision)Where:precision is an integer specifying the number of significant digits.Example: In below example we are using the toPrecision() Method to format a number with two decimals. JavaScript let num = 5438.876; let formattedNum = num.toPrecision(6); console.log(formattedNum); Output5438.88 Using Mathematical Rounding with Math.round()In this approach we have used the Math.round() method. It rounds a number to the nearest integer. To round a number to two decimal places we can manipulate the number before and after using Math.round(). By multiplying the number by 100, rounding it, and then dividing by 100, we effectively round the number to two decimal places.Syntax:Math.round((number + Number.EPSILON) * 100) / 100Example: In below example we are using the Math.round() method to format a number with two decimals. JavaScript let num1 = 123.456789; let formattedNum1 = Math.round((num1 + Number.EPSILON) * 100) / 100; console.log(formattedNum1); let num2 = 2343.657321; let formattedNum2 = Math.round((num2 + Number.EPSILON) * 100) / 100; console.log(formattedNum2); Output123.46 2343.66 Comment More infoAdvertise with us Next Article How to Format a Number with Two Decimals in JavaScript? G ghuleyogesh Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Parse Float with Two Decimal Places in JavaScript? JavaScript provides an inbuilt parseFloat() method to parse a string and returns a floating-point number. Below are the approaches to parse float to two decimal places in JavaScript:Table of ContentUsing parseFloat() MethodUsing Custom parseFloat Method Approach 1: Using parseFloat() MethodThe parse 2 min read How to Validate Decimal Numbers in JavaScript ? Validating user input is an essential aspect of Web Development. As a developer, when we are playing with the numeric inputs provided by the end-user, it is quite important to ensure that the input provided by the user is in the correct format. We can use the regular expression to Validate Decimal N 2 min read How to format numbers as currency string in JavaScript ? A number, represented as monetary value, creates an impact and becomes much more readable, and that's the reason behind formatting a number as currency. For example, a number, let's say 100000 when represented as $100,000.00 it becomes pretty much understood that it represents a monetary value, and 3 min read How to Remove the Decimal Part of a Number in JavaScript ? The decimal portion of a number in JavaScript can be removed through various methods, including the Math. trunc( ) method, Math. floor( ) method, Math. ceil( ) method, and Math. round( ) method. SyntaxMath.trunc(11.5) // Output: 11Math.floor(12.5) // Output: 12Math.ceil(15.5) // Output: 15Math.round 2 min read How to get decimal portion of a number using JavaScript ? Given a float number, The task is to separate the number into integer and decimal parts using JavaScript. For example, a value of 15.6 would be split into two numbers, i.e. 15 and 0.6 Here are a few methods discussed. These are the following methods: Table of Content Javascript String split() Method 3 min read How to Restrict Input to Numbers and Decimals in JavaScript? Restricting an input box to allow only numbers and decimal points involves setting up validation rules that ensure users can only enter numerical values and decimal separators. This helps maintain data integrity and prevents invalid entries in forms or user input fields.Below are the approaches to r 3 min read How to Format a Date in JavaScript? Here are different ways to format the date in JavaScript.1. Using the toDateString() MethodThe toDateString() method formats the date object into a human-readable format as Day Month Date Year.SyntaxdateObj.toDateString();JavaScriptconst date = new Date(); const formatDate = date.toDateString(); con 2 min read How to Generate a Random Number in JavaScript? To generate a random number in JavaScript, use the built-in methods that produce a floating-point number between 0 (inclusive) and 1 (exclusive).Below are the approaches to generate random numbers in JavaScript:Table of ContentUsing Math.random() methodUsing Math.floor() with Math.random()Using Math 2 min read How to Convert a Float Number to the Whole Number in JavaScript? Given a float number and the task is to convert a float number to a whole number using JavaScript. Below are various methods to convert float numbers to whole numbers in JavaScript:Table of ContentMath.floor (floating argument)Math.ceil (floating argument) Math.round (floating argument)Math.trunc (f 4 min read How to Format a Float in JavaScript? Formatting a float number means rounding off a number up to a given number of decimal places. Below are the approaches to format a float in JavaScript: Table of ContentUsing Math.ceil() methodUsing toFixed() methodUsing Math.round() methodUsing Math.floor() MethodUsing float.toExponential() methodUs 3 min read Like