Let’s do round a number to the nearest integer in js:-
Simply automatic will round up integer (number). If the first decimal above and equal the 5.
ceil() Method also Round a number upward to its nearest integer:
var num = 98.33; var n = Math.ceil(num)
Same as round up not need specials instructions. If first decimal is below 5.
floor() Method also can use to Round a number downward to its nearest integer:
Round to 2 decimals
Math.round(num * 100) / 100
Round to 1 decimal
Math.round(num * 10) / 10
Round to nearest 10
var number = 33; </code> <code>console.log(Math.round(number / 10) * 10);
Round to 4 decimal places
(6.688689).toFixed(); // equal to 7 (6.688689).toFixed(1); // equal to 6.7 (6.688689).toFixed(2); // equal to 6.69
Do comment if you have any doubts and suggestion on topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.