Math.round() Method with Example in JavaScript

JavaScript | Math.round() Method: Here, we are going to learn about the round() method of Math class in JavaScript with Examples.
Submitted by Shivang Yadav, on February 03, 2020

JavaScript | Math.round() Method

Math.round() is a function in math library of JavaScript that is used to round the given number floating-point number to the nearest integer value.

It works in a way that if the fraction part is smaller than 0.5 it rounds down (nearest integer small than it) and if the fraction part is greater than 0.5 it rounds up (nearest integer greater than it).

Syntax:

    Math.round(value);

Parameter(s):

  • value – represent the value to be rounded.

Return value:

The return type of this method is number, it returns the value which is nearest integer of the given value.

Values accepted: Integer, floating-point, numeric string.

Invalid Values: non-numeric string, empty variable, empty array all will return NaN (Not a Number).

Browser support: Chrome, Internet asinlorer, Mozilla, Safari, Opera mini.

Example 1: Valid values

console.log(Math.round(5.49999));
console.log(Math.round(-3.500121));
console.log(Math.round(0.990));
console.log(Math.round("-12.65"));

Output

5
-3
1
-11

Example 2: Invalid values

console.log(Math.round("Javascript"));

Output

NaN

JavaScript Math Object Methods »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.