The Wayback Machine - https://web.archive.org/web/20241001011654/https://www.geeksforgeeks.org/javascript-math-round-function-2/
Open In App

JavaScript Math.round( ) function

Last Updated : 22 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript Math.round( ) function is used to round the number passed as a parameter to its nearest integer. 

Syntax:

Math.round(value)

Parameters:

  • value: The number to be rounded to its nearest integer.

Example 1: Rounding Off a number to its nearest integer

To round off a number to its nearest integer, the math.round() function should be implemented in the following way: 

Javascript




let round = Math.round(5.8);
console.log("Number after rounding : " + round);


Output:

Number after rounding : 6

Example 2: Rounding Off a negative number to its nearest integer

The Math.round() function itself rounds off a negative number when passed as a parameter to it. To round off a negative number to its nearest integer, the Math.round() function should be implemented in the following way: 

Javascript




let round = Math.round(-5.8);
console.log("Number after rounding : " + round);


Output:

Number after rounding : -6

Example 3: Math.round() function, when a parameter has “.5” as a decimal

Below program shows the result of Math.round() function when the parameter has “.5” as a decimal. 

Javascript




let round = Math.round(-12.5);
console.log("Number after rounding : " + round);
let round2 = Math.round(12.51);
console.log("Number after rounding : " + round2);


Output:

Number after rounding : -12
Number after rounding : 13

We have a complete list of Javascript Math Objects methods, to check those please go through this Javascript Math Object Complete reference article.

Supported Browsers:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 3 and above
  • Opera 3 and above
  • Safari 1 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Similar Reads

How to Round up/ Round down a Moment to Nearest Minute?
To round a moment object to the nearest minute using Moment.js, and check if the seconds are less than 30 to determine if you should round down to the start of the minute or round up to the next minute. Below are the approaches to round up/down a moment.js moment to the nearest minute: Table of Content Rounding Up (Ceil) Rounding Down (Floor) Round
2 min read
Less.js Math round() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is an extension to normal CSS which basically enhances the abilities of normal CSS and gives superpowers to it. LESS.js provides the built-in Math function that can be used to customize the CSS properties, whic
3 min read
JavaScript Math round() Method
The JavaScript Math.round() method rounds a number to the nearest integer. It rounds up for decimal values of 0.5 and higher, and down otherwise. Syntax:Math.round(value);Parameters:value: It is the number that you want to round off.Return Value:The Math.round() method returns the value of the given number rounded to the nearest integer. Example 1:
3 min read
PHP Math Functions (abs, pi, deg2rad, rad2deg, fmod, floor, ceil, round, is_finite, is_infinite)
As you know PHP is a server side script language and is used for creating dynamic web pages. PHP provides a number of built-in math functions which help in performing several operations while dealing with mathematical data. It has nice support for mathematical processing. No installation is required to use these functions. Some examples of mathemat
4 min read
PHP round( ) Function
While dealing with problems which have values consisting of a very high number of decimal digits such as (121.76763527823) we often come up with a problem of rounding them up. Rounding them up manually can be a very time consuming and erroneous practice. In place of that, an inbuilt function of PHP i.e round() can be used. The round() function in P
3 min read
p5.js round() function
The round() function in p5.js is used to calculate the round value of a number. It calculates the integer closest to the number. Syntax round(number) Parameters: The function accepts only one parameter as mentioned above and described below: number : This parameter stores the number to compute. Below program illustrates the round() function in p5.j
1 min read
D3.js band.round() Function
The band.round() function is used to set the round true or false i.e it enables or disables the rounding. If the round is et to true then the start and stop of each band will be an integer. Syntax: band.round([round]); Parameters: This function accepts single parameters as given above and described below: round: This parameter accepts a boolean val
2 min read
Tensorflow.js tf.round() Function
Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .round() function is used to find the round value of the stated tensor input, and it is done element wise. Moreover, it is helpful in implementing banker's rou
2 min read
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
2 min read
Round off a number to the next multiple of 5 using JavaScript
In this article, we are given a positive integer n and the task is to round the number to the next whole number divisible by 5. There are various methods to Round off a number to the next multiple of 5 using JavaScript: Using Math.ceil() methodUsing Math.floor() method Examples: Input : 46 Output : 50 Input : 21 Output : 25 Input : 30 Output : 30 A
1 min read
How to Round off Time to Nearest 5 Min using JavaScript ?
Given a JavaScript date and the task is to round it to 5 minutes with the help of JavaScript. There are two approaches that are discussed below: Approach 1: In this approach, both options are available to either round down or round up the date object. This example uses basic Math.floor() function and Math.ceil() function to perform the operation. E
2 min read
How to Round Time to the Nearest Quarter Hour using JavaScript ?
We have given a time and the task is to round the time to the nearest quarter-hour with the help of JavaScript. There are two approaches that are discussed below: Approach 1: Use getMinutes() and getHours() methods to get the minutes and hours in a variable (Ex. mins, hrs) add the 7.5 to mins, divide it by 15, take the integer value and multiply it
3 min read
How to make a Round Slider using JavaScript ?
Round Slider is the UI element that allows us to select a value by dragging a handle along the circumference of the circular track. The below approaches can be used to accomplish this task. Table of Content Using CanvasUsing jQueryUsing CanvasIn this example, we're creating a round slider using the HTML5 Canvas element. The slider allows us to inte
4 min read
How to Round a Number to a Certain Number of Decimal Places in JavaScript ?
Rounding a number to a certain number of decimal places in JavaScript means adjusting the number to a specified precision after the decimal point. This is commonly done using methods like toFixed() or Math.round() to format the number for precise calculations or display. Here's how you can achieve it using these methods: Using toFixed() methodThe t
1 min read
JavaScript Math cbrt() Function
The Javascript Math.cbrt() function is used to find the cube root of a number. The cube root of a number is denoted as Math.cbrt(x)=y such that y^3=x. Syntax: Math.cbrt(number) Parameters: This function accepts a single parameter. number: The number whose cube root you want to know. Return Value: It returns the cube root of the given number. The be
1 min read
JavaScript Math.clz32() Function
The Math.clz32( ) function in JavaScript returns the number of leading zero bits in the 32-bit binary representation of a number. Clz32 stands for Count Leading Zeros 32. If the passed parameter is not a number, then it will be converted into a number first and then converted to a 32-bit unsigned integer. If the converted 32-bit unsigned integer is
2 min read
JavaScript Math.atanh() Function
The Javascript Math.atanh() function is an inbuilt function in JavaScript that is used to get the hyperbolic arctangent of a number. The hyperbolic arctangent is known by many names such as hyperbolic inverse tangent and atanh. It is the inverse of the hyperbolic tangent function i.e, The inverse hyperbolic tangent of any value says x is the value
2 min read
What is the use of the Math.random Function in JavaScript ?
The Math.random() function in JavaScript is used to generate a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). Example 1: Here, Math.random() is used to generate a random number. C/C++ Code let randomNumber = Math.random(); // Outputs a random number between 0 and 1 (excluding 1) console.log(randomNumber); Output0.76204
1 min read
What is the use of the Math.random() function in JavaScript ?
The Math.random() function is used to generate a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive). Syntax:Math.random();Example: Here, Math.random() generates a random number, which could be any floating-point value between 0 (inclusive) and 1 (exclusive). This random number is then stored in the variable randomNumber, an
1 min read
JavaScript Math.ceil( ) function
The JavaScript Math.ceil() function rounds a given number up to the nearest integer. It always rounds towards positive infinity, meaning it increases the number to the next whole number if it's not already an integer. This function is useful for rounding up values in calculations. SyntaxMath.ceil(value);Parameters: This function accepts a single pa
2 min read
Goldman Sachs Interview Experience | Coding Round
Coding Round had 4 sections and time duration was 2 hours: Section 1: 2 coding questions. Question 1 was to find lexicographically smallest sub-sequence of a given length k from a given string of non-capital alphabets. Eg, if string is bacb and k = 2, then output will be ab. Question 2: An institute provides 3 cricket courses: A, B, and C. Each cou
2 min read
How to place SVG icons on a round circle side by side to another div using Bootstrap?
Bootstrap Icons are SVGs, so they scale quickly and easily and can be styled with CSS. Approach: We can place the SVG icons and give them the shape of a circle. This will make the SVG icons appear to be in a circular shape. This can be achieved using a Bootstrap class called "rounded-circle" Syntax: <img class = "rounded-circle" src = "..." alt
2 min read
W3.CSS Round
Border radius is used to make the corner of the border curved. More the radius, more curved and round it will be. These classes can be applied to any division, image, buttons, containers, etc. Sr. No. Class Description 1. w3-round-small It sets the border-radius of the element to 2px. 2. w3-round It sets the border-radius of the element to 4px. 3.
2 min read
Moment.js Round Plugin
The Round plugin is a moment.js plugin that will round the date and time to the specified range. Write the below command in the terminal to install Round Plugin: npm install moment-round The following are some of the functions in this plugin: roundfloorceil The below examples will help to understand some of the methods of the Timer Plugin. Example
1 min read
How to style round buttons using CSS ?
This article will explore how to style round buttons using CSS. Round buttons give a more visually pleasing design and attract user attention compared to default rectangular buttons. We can achieve the rounded button effect by applying CSS property. Rounded buttons are responsible for enhancing the overall user experience. Preview ApproachMake a ba
2 min read
How to Round MomentJS Time to Nearest 30-Minute Interval?
If we want to round the time to the nearest 30-minute interval, we can do this in JavaScript with the help of MomentJS. Rounding time to the nearest 30-minute interval is used to schedule tasks, book appointments, etc. Suppose we have an appointment at 10:22 AM, we might want to round it to 10:30 AM. Or if we have a task scheduled at 3:47 PM, you m
2 min read
Lodash _.round() Method
Lodash _.round() method is used to compute numbers rounded to precision. Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. Syntax:_.round(number, [precision = 0])Parameters: This method accepts two parameters as mentioned above and described below: number: Thi
2 min read
PHP base_convert( ) Math Function
The base_convert() function in PHP is used to convert a number given in an arbitrary base to a desired base. Both the base should be between 2 and 32 and bases with digits greater than 10 are represented with letters a-z i.e 10 is represented as a, 11 is represented as b and 35 is represented as z. The case of the letters is not sensitive. Syntax:
2 min read
Math.js compile() Function
The Math.js is an extensive library with mathematical functions that work on JavaScript and Node.js. The additional feature of this library is that it provides a flexible expression parser that supports symbolic computation. It is quite powerful and easy to use since it comes with many built-in functions and offers data types such as fractions, com
2 min read
Less.js Math pow() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is chosen because CSS is a dynamic style sheet language. LESS is flexible, thus it functions with a variety of browsers. Web browsers can only use CSS that has been written in and processed using the CSS pre-pr
3 min read