round decimal to whole number javascript code example

Example 1: math.round js 1 decimal

var number = 12.3456789;
var rounded = Math.round( number * 10 ) / 10;
// rounded is 12.3

Example 2: js rounding

//There are meny ways of rounding...
Math.floor(5.5) //Answer 5, it alwas rounds down.
Math.round(5.5) //Answer 6, it simpily rounds to the closest whole number.
Math.ceil(5.5) //Answer 6, it alwas rounds up.

//You can do more things too...
Math.floor(5.57 * 10) / 10 //Answer 5.5, the number turns into 55.7, Then gets floored (55.0), Then gets divied, (5.5).

Example 3: rounding number to x decimals javascript

let num = 12.5452411;
num = num.toFixed(3); // 12.545

Example 4: round number 2 decimals javascript

Math.round((num + Number.EPSILON) * 100) / 100

Example 5: javascript round to 7 decimal places

myNumber.toFixed(7);

Example 6: round decimal js

Math.round(num * 100) / 100