Math Round Function of Javascript returns the rounded integer value nearest to a number passed to the Math.round(x) function. Math round function rounds off the precision point values, if the last decimal point value is greater than or equal to 5 then it increases the value of previous precision place value by 1 and so on to first precision point value. At last Math.round function checks that if the decimal point value is greater than or equal to 5 then it returns then integer by adding 1 to the decimal number at the left side of the decimal point. Math.round function starts this round off process from extreme right precision point value to the left most values. If precision point value is less than 5 then the point value at its previous place remains unchanged.
Syntax for Javascript Math Round Function
Math.round(x);
Math.round function accepts the value as number and returns the rounded integer value for it.
Examples of Javascript Math Round Function
Place this example code in your HTML page inside the HTML body or Head Section of the HTML document:
document.write(Math.round(0.51) + "<br />"); document.write(Math.round(0.44) + "<br />"); document.write(Math.round(2.44) + "<br />"); document.write(Math.round(-0.44) + "<br />"); document.write(Math.round(-0.51) + "<br />"); document.write(Math.round(-2.44) + "<br />"); // Output: // 1 // 0 // 2 // 0 // -1 // -2
Continue to next tutorial: Javascript Math Ceil Function to learn how to get the nearest greater value to the specified number.