The Math object is used to perform Math tasks. Math objects are not classes of objects like Date and String, so there is no constructor Math().
###Math object property
###Math object method
###ceil() The ceil() method rounds up a number.
# # # syntax math.h ceil (x)
The 1.x parameter is required. It has to be a number.
TIPS It returns the nearest integer greater than or equal to x.
The instance
<! DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; Charset = utF-8 "/> <title>ceil()</title> <script type="text/javascript"> document.write(math.ceil (3.3)); charset= utF-8" /> <title> <script type="text/javascript"> document.write(math.ceil (3.3)); Document. The write (math.h ceil (0.1)); </script> </head> <body> </body> </html>Copy the code
###floor() The floor() method rounds down a number.
# # # syntax math.h floor (x)
The 1.x parameter is required. Any value or expression.
TIPS returns the integer that is less than or equal to x and closest to x.
The instance
<! DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; Charset = UTF-8 "/> <title>floor()</title> <script type="text/javascript"> document.write(math.floor (3.3)); charset= UTF-8" /> <title> <script type="text/javascript"> document.write(math.floor (3.3)); Document. The write (Math. Floor (0.1)); </script> </head> <body> </body> </html>Copy the code
The round() method rounds a number to the nearest whole number.
# # # syntax Math. Round (x)
The 1.x parameter is required. It has to be a number.
TIPS 1. Return the nearest integer to x. 2. For 0.5, this method will be rounded up. 3. If x is equally close to the integers on both sides, the result is close to the numerical value in the direction of +∞. (If -5.5 is rounded to -5; -5.52 will be rounded to -6)
The instance
<! DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; Charset = utF-8 "/> <title>round()</title> <script type="text/javascript"> document.write(math.round (3.3)); charset= utF-8" /> <title> <script type="text/javascript"> document.write(math.round (3.3)); Document. The write (Math. Round (0.1)); Document. The write (Math. Round (9.9)); Document. The write (Math. Round (8.9)); </script> </head> <body> </body> </html>Copy the code
The random() method returns a random number between 0 and 1 (greater than or equal to 0 but less than 1).
# # 3 grammar Math. The random ();
TIPS returns a numeric value with a positive sign greater than or equal to 0 but less than 1.
The instance
<! DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Math </title> <script type="text/javascript"> document.write(Math.round((Math.random())*10)); </script> </head> <body> </body> </ HTML >Copy the code