{"id":31,"date":"2007-07-20T18:01:26","date_gmt":"2007-07-20T22:01:26","guid":{"rendered":"http:\/\/prglab.com\/blog\/js-math"},"modified":"2007-08-25T14:15:11","modified_gmt":"2007-08-25T18:15:11","slug":"js-math","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/js-tutorial\/js-math","title":{"rendered":"JavaScript \u7b97\u672f\u5bf9\u8c61(Math Object)"},"content":{"rendered":"<p>\u7b97\u672f\u5bf9\u8c61\u53ef\u7528\u6765\u5b8c\u6210\u901a\u7528\u7684\u7b97\u672f\u8ba1\u7b97\u4efb\u52a1\u3002<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<h2>\u7b97\u672f\u5bf9\u8c61(Math Object)<\/h2>\n<p>\u7b97\u672f\u5bf9\u8c61\u53ef\u7528\u6765\u5b8c\u6210\u901a\u7528\u7684\u7b97\u672f\u8ba1\u7b97\u4efb\u52a1\u3002<\/p>\n<p>\u7b97\u672f\u5bf9\u8c61\u5305\u62ec\u82e5\u5e72\u7b97\u672f\u503c\u548c\u51fd\u6570\u3002\u4f7f\u7528JS\u4e2d\u7b97\u672f\u5bf9\u8c61\u4e0d\u9700\u8981\u4e8b\u5148\u5b9a\u4e49\u3002<\/p>\n<hr \/>\n<h2>\u7b97\u672f\u503c<\/h2>\n<p>JavaScript \u63d0\u4f9b8\u4e2a\u7b97\u672f\u503c(\u5e38\u91cf)\uff0c\u53ef\u901a\u8fc7Math\u5bf9\u8c61\u76f4\u63a5\u8bbf\u95ee\u3002\u5b83\u4eec\u662f\uff1aE, PI, 2\u7684\u5e73\u65b9\u6839\uff0c1\/2\u7684\u5e73\u65b9\u6839\uff0c2\u7684\u81ea\u7136\u5bf9\u6570\uff0c10\u7684\u81ea\u7136\u5bf9\u6570\uff0c\u4ee52\u4e3a\u5e9510\u7684\u5bf9\u6570\uff0c\u548c\u4ee510\u4e3a\u5e95Ed\u7684\u5bf9\u6570\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u7528\u4e0b\u5217\u8868\u8fbe\u5f0f\u8c03\u7528\u8fd9\u4e9b\u503c\uff1a<\/p>\n<table class=\"ex\" id=\"table11\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\nMath.E<br \/>\nMath.PI<br \/>\nMath.SQRT2<br \/>\nMath.SQRT1_2<br \/>\nMath.LN2<br \/>\nMath.LN10<br \/>\nMath.LOG2E<br \/>\nMath.LOG10E\n<\/td>\n<\/tr>\n<\/table>\n<hr \/>\n<h2>\u7b97\u672f\u51fd\u6570<\/h2>\n<p>\u9664\u7b97\u672f\u503c\u5916\uff0c\u7b97\u672f\u5bf9\u8c61\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e9b\u7b97\u672f\u51fd\u6570\u3002<\/p>\n<p><strong>\u51fd\u6570\u4e3e\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u4f8b\u5b50\u4f7f\u7528\u7b97\u672f\u5bf9\u8c61\u7684\u51fd\u6570round()\u6765\u5c06\u4e00\u4e2a\u6570\u503c\u56db\u820d\u4e94\u5165\u53d6\u6574\uff1a<\/p>\n<table class=\"ex\" id=\"table1\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\ndocument.write(Math.round(4.7))\n<\/td>\n<\/tr>\n<\/table>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a<\/p>\n<table class=\"ex\" id=\"table2\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\n5\n<\/td>\n<\/tr>\n<\/table>\n<p>\u4ee5\u4e0b\u4f8b\u5b50\u4f7f\u7528\u7b97\u672f\u5bf9\u8c61\u7684random() \u51fd\u6570\u6765\u8fd4\u56de\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6570\u503c\uff1a<\/p>\n<table class=\"ex\" id=\"table5\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\ndocument.write(Math.random())\n<\/td>\n<\/tr>\n<\/table>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u53ef\u80fd\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa\uff1a<\/p>\n<table class=\"ex\" id=\"table6\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\n<script type=\"text\/javascript\"> \t\ndocument.write(Math.random()) \t\n<\/script>\n<\/td>\n<\/tr>\n<\/table>\n<p>\u4e0b\u4f8b\u4f7f\u7528\u7b97\u672f\u5bf9\u8c61\u7684floor() \u548crandom() \u51fd\u6570\u6765\u8fd4\u56de0\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff1a<\/p>\n<table class=\"ex\" id=\"table9\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\ndocument.write(Math.floor(Math.random()*11))\n<\/td>\n<\/tr>\n<\/table>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u53ef\u80fd\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa:<\/p>\n<table class=\"ex\" id=\"table10\" border=\"1\" cellspacing=\"0\" width=\"100%\">\n<tr>\n<td>\n<script type=\"text\/javascript\"> \t\ndocument.write(Math.floor(Math.random()*11)) \t\n<\/script>\n<\/td>\n<\/tr>\n<\/table>\n<hr \/>\n<p><a href=\"http:\/\/www.w3schools.com\/js\/js_obj_boolean.asp\"><\/a>\u6b64\u5916\u8fd8\u6709max()\uff0cmin()\uff0clog()\uff0csin()\uff0ccos()\uff0cpow()\uff0csqrt()\u7b49\u51fd\u6570\uff0c\u8fd9\u91cc\u4e0d\u518d\u4e00\u4e00\u4e3e\u4f8b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b97\u672f\u5bf9\u8c61\u53ef\u7528\u6765\u5b8c\u6210\u901a\u7528\u7684\u7b97\u672f\u8ba1\u7b97\u4efb\u52a1\u3002 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; \u7b97\u672f\u5bf9\u8c61(Math Object) \u7b97\u672f\u5bf9\u8c61\u53ef\u7528\u6765\u5b8c\u6210\u901a\u7528\u7684\u7b97\u672f\u8ba1\u7b97\u4efb\u52a1\u3002 \u7b97\u672f\u5bf9\u8c61\u5305\u62ec\u82e5\u5e72\u7b97\u672f\u503c\u548c\u51fd\u6570\u3002\u4f7f\u7528JS\u4e2d\u7b97\u672f\u5bf9\u8c61\u4e0d\u9700\u8981\u4e8b\u5148\u5b9a\u4e49\u3002 \u7b97\u672f\u503c JavaScript \u63d0\u4f9b8\u4e2a\u7b97\u672f\u503c(\u5e38\u91cf)\uff0c\u53ef\u901a\u8fc7Math\u5bf9\u8c61\u76f4\u63a5\u8bbf\u95ee\u3002\u5b83\u4eec\u662f\uff1aE, PI, 2\u7684\u5e73\u65b9\u6839\uff0c1\/2\u7684\u5e73\u65b9\u6839\uff0c2\u7684\u81ea\u7136\u5bf9\u6570\uff0c10\u7684\u81ea\u7136\u5bf9\u6570\uff0c\u4ee52\u4e3a\u5e9510\u7684\u5bf9\u6570\uff0c\u548c\u4ee510\u4e3a\u5e95Ed\u7684\u5bf9\u6570\u3002 \u4f60\u53ef\u4ee5\u7528\u4e0b\u5217\u8868\u8fbe\u5f0f\u8c03\u7528\u8fd9\u4e9b\u503c\uff1a Math.E Math.PI Math.SQRT2 Math.SQRT1_2 Math.LN2 Math.LN10 Math.LOG2E Math.LOG10E \u7b97\u672f\u51fd\u6570 \u9664\u7b97\u672f\u503c\u5916\uff0c\u7b97\u672f\u5bf9\u8c61\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e9b\u7b97\u672f\u51fd\u6570\u3002 \u51fd\u6570\u4e3e\u4f8b\uff1a \u4ee5\u4e0b\u4f8b\u5b50\u4f7f\u7528\u7b97\u672f\u5bf9\u8c61\u7684\u51fd\u6570round()\u6765\u5c06\u4e00\u4e2a\u6570\u503c\u56db\u820d\u4e94\u5165\u53d6\u6574\uff1a document.write(Math.round(4.7)) \u4ee5\u4e0a\u4ee3\u7801\u5c06\u8f93\u51fa\uff1a 5 \u4ee5\u4e0b\u4f8b\u5b50\u4f7f\u7528\u7b97\u672f\u5bf9\u8c61\u7684random() \u51fd\u6570\u6765\u8fd4\u56de\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6570\u503c\uff1a document.write(Math.random()) \u4ee5\u4e0a\u4ee3\u7801\u53ef\u80fd\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa\uff1a \u4e0b\u4f8b\u4f7f\u7528\u7b97\u672f\u5bf9\u8c61\u7684floor() \u548crandom() \u51fd\u6570\u6765\u8fd4\u56de0\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff1a document.write(Math.floor(Math.random()*11)) \u4ee5\u4e0a\u4ee3\u7801\u53ef\u80fd\u4ea7\u751f\u4ee5\u4e0b\u8f93\u51fa: \u6b64\u5916\u8fd8\u6709max()\uff0cmin()\uff0clog()\uff0csin()\uff0ccos()\uff0cpow()\uff0csqrt()\u7b49\u51fd\u6570\uff0c\u8fd9\u91cc\u4e0d\u518d\u4e00\u4e00\u4e3e\u4f8b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3,"menu_order":23,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/31"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":0,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/31\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/3"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}