Math.round()
14th January 2021
0 Comments
Programming Language
JavaScript [Version: ECMAScript (ECMA-262)]
Syntax
Typical Usage
Returns the value of a number rounded to the nearest integer.
Accepted Parameters
- value (float) (Required): The decimal number you wish to round off.
Return Value(s)
Returns the rounded number.Notes
If the fractional portion of the value is greater than 0.5, the value is rounded to the next integer. If it is less than 0.5, the argument is rounded to the lower integer. If the fractional portion is exactly 0.5, the argument is rounded to the next integer in the direction of +∞. This differs from some other languages’ round() functions, which often round to the next integer away from zero, instead giving a different result in the case of negative numbers with a fractional part of exactly 0.5.
Examples of Math.round()
Example 1
console.log(Math.round(2.1234));
The above will output 2.
Example 2
console.log(Math.round(68.7654));
The above will output 69.
Example 3: Use Math.round to Round to 2 Decimal Places
console.log(Math.round(68.7654* 100) / 100)
The above code will output 68.77. To round to 3 digits, for example, replace 100 with 1000.
Similar Functions in Other Languages
- PHP: round()
Was This Helpful?
If this post has helped you, please let me know. Your feedback will be highly appreciated and will help me build better content.