15 1 0 4000 1 https://codeblock.co.za 300 true 0

Math.round()

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

Was This Helpful?

Previous Post
Math.floor()
Next Post
console.log()

0 Comments

Leave a Reply