Home » JavaScript Tutorial

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.