{"id":164645,"date":"2024-05-08T14:58:52","date_gmt":"2024-05-08T06:58:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/164645.html"},"modified":"2024-05-08T14:58:57","modified_gmt":"2024-05-08T06:58:57","slug":"sqrt-%e6%96%b9%e6%b3%95%e5%a6%82%e4%bd%95%e5%9c%a8-javascript-%e9%a1%b9%e7%9b%ae%e7%a8%8b%e5%ba%8f%e4%b8%ad%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/164645.html","title":{"rendered":"sqrt \u65b9\u6cd5\u5982\u4f55\u5728 javascript \u9879\u76ee\u7a0b\u5e8f\u4e2d\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26231519\/b392ab64-e2f3-42ec-abdc-2dfae280cdc3.webp\" alt=\"sqrt \u65b9\u6cd5\u5982\u4f55\u5728 javascript \u9879\u76ee\u7a0b\u5e8f\u4e2d\u4f7f\u7528\" \/><\/p>\n<p><p>JavaScript \u4e2d\u7684 <code>Math.sqrt<\/code> \u65b9\u6cd5\u7528\u4e8e\u8ba1\u7b97\u53c2\u6570\u7684\u5e73\u65b9\u6839\u3002\u7b80\u5355\u5730\u8bf4\uff0c\u5982\u679c\u8981\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\uff0c\u53ea\u9700\u8c03\u7528 <code>Math.sqrt<\/code> \u5e76\u4f20\u5165\u76f8\u5e94\u7684\u6570\u5b57\u5373\u53ef\u3002\u4f8b\u5982\uff0c<code>Math.sqrt(9)<\/code> \u8fd4\u56de <code>3<\/code>\uff0c\u56e0\u4e3a <code>3 * 3<\/code> \u7b49\u4e8e <code>9<\/code>\u3002\u8fd9\u4e00\u65b9\u6cd5\u5728\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u3001\u79d1\u5b66\u8ba1\u7b97\u3001\u7edf\u8ba1\u5206\u6790\u7b49\u573a\u666f\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<strong>\u4f7f\u7528\u65f6\uff0c\u8981\u8003\u8651\u8fb9\u754c\u60c5\u51b5\u3001\u8f93\u5165\u503c\u7684\u9a8c\u8bc1\u4ee5\u53ca\u5bf9\u4e8e\u7279\u6b8a\u8f93\u5165\uff08\u5982\u8d1f\u6570\u6216\u975e\u6570\u5b57\uff09\u7684\u5904\u7406<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba <code>Math.sqrt<\/code> \u65b9\u6cd5\u7684\u4f7f\u7528\u573a\u666f\u3001\u5b9e\u9645\u4ee3\u7801\u5b9e\u73b0\u4ee5\u53ca\u9700\u8981\u6ce8\u610f\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p><strong>Math.sqrt \u65b9\u6cd5\u7684\u8bed\u6cd5\u548c\u57fa\u672c\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p><code>Math.sqrt<\/code> \u65b9\u6cd5\u662f JavaScript \u7684 <code>Math<\/code> \u5bf9\u8c61\u7684\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u6765\u8ba1\u7b97\u5e76\u8fd4\u56de\u4efb\u4f55\u975e\u8d1f\u6570\u503c\u7684\u5e73\u65b9\u6839\u3002\u57fa\u672c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let result = Math.sqrt(number);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>number<\/code> \u662f\u8981\u8ba1\u7b97\u5e73\u65b9\u6839\u7684\u975e\u8d1f\u6570\u3002<\/p>\n<\/p>\n<p><p>\u5982\u679c <code>number<\/code> \u662f\u8d1f\u6570\uff0c\u5219 <code>Math.sqrt<\/code> \u8fd4\u56de <code>NaN<\/code>\uff08\u4e0d\u662f\u4e00\u4e2a\u6570\u5b57\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let squareRootOfNegative = Math.sqrt(-1); \/\/ NaN<\/p>\n<p>let squareRootOfPositive = Math.sqrt(16); \/\/ 4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5728\u51fd\u6570\u4e2d\u5c01\u88c5 Math.sqrt<\/strong><\/p>\n<\/p>\n<p><p>\u60a8\u53ef\u4ee5\u5728\u51fd\u6570\u5185\u90e8\u8c03\u7528 <code>Math.sqrt<\/code> \u65b9\u6cd5\u6765\u6267\u884c\u66f4\u590d\u6742\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\uff0c\u5904\u7406\u7528\u6237\u8f93\u5165\u6216\u5728\u6570\u7ec4\u4e2d\u5e94\u7528\u5e73\u65b9\u6839\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function getSquareRoot(value) {<\/p>\n<p>    if (typeof value !== &#039;number&#039; || value &lt; 0) {<\/p>\n<p>        return NaN;<\/p>\n<p>    }<\/p>\n<p>    return Math.sqrt(value);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u590d\u6742\u6570\u503c\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p><code>Math.sqrt<\/code> \u65b9\u6cd5\u7ecf\u5e38\u7528\u4e8e\u6c42\u89e3\u6570\u5b66\u95ee\u9898\uff0c\u4f8b\u5982\u5728\u89e3\u51b3\u4e8c\u6b21\u65b9\u7a0b\u6216\u4f7f\u7528\u6bd5\u8fbe\u54e5\u62c9\u65af\u5b9a\u7406\u8ba1\u7b97\u76f4\u89d2\u4e09\u89d2\u5f62\u5176\u4e2d\u4e00\u8fb9\u7684\u957f\u5ea6\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u4f7f\u7528 Math.sqrt \u8ba1\u7b97\u76f4\u89d2\u4e09\u89d2\u5f62\u7684\u659c\u8fb9<\/p>\n<p>function calculateHypotenuse(a, b) {<\/p>\n<p>    return Math.sqrt(a * a + b * b);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u7edf\u8ba1\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p><code>Math.sqrt<\/code> \u4e5f\u5e38\u89c1\u4e8e\u7edf\u8ba1\u5206\u6790\u4e2d\uff0c\u5982\u6807\u51c6\u5dee\u7684\u8ba1\u7b97\uff0c\u6216\u8005\u5728\u7f6e\u4fe1\u533a\u95f4\u7684\u786e\u5b9a\u4e2d\u8ba1\u7b97\u6807\u51c6\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">\/\/ \u8ba1\u7b97\u6807\u51c6\u5dee<\/p>\n<p>function calculateStandardDeviation(values) {<\/p>\n<p>    let mean = values.reduce((sum, value) =&gt; sum + value, 0) \/ values.length;<\/p>\n<p>    let variance = values.reduce((varianceSum, value) =&gt; varianceSum + Math.pow(value - mean, 2), 0) \/ values.length;<\/p>\n<p>    return Math.sqrt(variance);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u53e6\u5916\uff0c\u5728\u7269\u7406\u5b66\u3001\u5de5\u7a0b\u548c\u5176\u4ed6\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c<code>Math.sqrt<\/code> \u65b9\u6cd5\u7ecf\u5e38\u88ab\u7528\u6765\u6c42\u89e3\u5177\u6709\u5e73\u65b9\u6839\u8fd0\u7b97\u7684\u516c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5148\u8fdb\u6280\u5de7<\/h3>\n<\/p>\n<p><p><strong>\u4f18\u5316\u6027\u80fd<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136 <code>Math.sqrt<\/code> \u672c\u8eab\u662f\u9ad8\u5ea6\u4f18\u5316\u7684\uff0c\u4f46\u5728\u6267\u884c\u5927\u91cf\u5e73\u65b9\u6839\u8ba1\u7b97\u65f6\uff0c\u4ecd\u7136\u53ef\u4ee5\u91c7\u53d6\u4e00\u4e9b\u63aa\u65bd\u6765\u63d0\u9ad8\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u7f13\u5b58\u5e38\u89c4\u7684\u8ba1\u7b97\u7ed3\u679c\u6216\u4f7f\u7528\u8fd1\u4f3c\u65b9\u6cd5\u6765\u4ee3\u66ff\u5b9e\u9645\u7684\u5e73\u65b9\u6839\u8ba1\u7b97\uff0c\u8fd9\u5728\u7cbe\u5ea6\u8981\u6c42\u4e0d\u9ad8\u7684\u60c5\u51b5\u4e0b\u5c24\u4e3a\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>\u5904\u7406\u975e\u6570\u503c\u548c\u8fb9\u754c\u60c5\u51b5<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u8fd8\u9700\u5bf9\u8f93\u5165\u503c\u8fdb\u884c\u68c0\u67e5\uff0c\u4ee5\u786e\u4fdd\u7b97\u6cd5\u7684\u5065\u58ee\u6027\u3002\u5f53 <code>Math.sqrt<\/code> \u63a5\u6536\u5230\u975e\u6570\u503c\u6216\u8d1f\u6570\u65f6\uff0c\u4f1a\u8fd4\u56de <code>NaN<\/code>\uff0c\u56e0\u6b64\u5e94\u8be5\u5b9e\u73b0\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u6d41\u7a0b\u4ee5\u907f\u514d\u8ba1\u7b97\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><p><strong>\u89e3\u51b3\u65b9\u6848\u4e00\uff1a\u5904\u7406\u8d1f\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5f53\u5c1d\u8bd5\u8ba1\u7b97\u4e00\u4e2a\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u65f6\uff0c<code>Math.sqrt<\/code> \u4f1a\u8fd4\u56de <code>NaN<\/code>\u3002\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e00\u79cd\u65b9\u6cd5\u662f\u5728\u4f20\u9012\u6570\u503c\u4e4b\u524d\u68c0\u67e5\u5176\u662f\u5426\u4e3a\u8d1f\u6570\uff0c\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u590d\u6570\uff08\u5728 JavaScript \u4e2d\u53ef\u4ee5\u901a\u8fc7\u5176\u4ed6\u5e93\u5b9e\u73b0\uff0c\u4f8b\u5982 <code>math.js<\/code>\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>SquareRoot(number) {<\/p>\n<p>    if (number &lt; 0) {<\/p>\n<p>        return &#039;Complex number&#039;;<\/p>\n<p>    }<\/p>\n<p>    return Math.sqrt(number);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u51b3\u65b9\u6848\u4e8c\uff1a\u9a8c\u8bc1\u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6765\u81ea\u52a8\u6001\u6570\u636e\u6e90\u6216\u7528\u6237\u8f93\u5165\u7684\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u4f20\u9012\u7ed9 <code>Math.sqrt<\/code> \u7684\u662f\u6b63\u786e\u6570\u636e\u7c7b\u578b\u81f3\u5173\u91cd\u8981\u3002\u8fd9\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 <code>typeof<\/code> \u8fdb\u884c\u7c7b\u578b\u68c0\u67e5\u6216\u8005\u4f7f\u7528 <code>Number()<\/code> \u5c06\u8f93\u5165\u8f6c\u6362\u4e3a\u6570\u503c\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function getValidSquareRoot(value) {<\/p>\n<p>    let number = Number(value);<\/p>\n<p>    if (isNaN(number) || number &lt; 0) {<\/p>\n<p>        return NaN;<\/p>\n<p>    }<\/p>\n<p>    return Math.sqrt(number);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u89e3\u51b3\u65b9\u6848\u4e09\uff1a\u6027\u80fd\u4f18\u5316<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u6216\u8fdb\u884c\u5927\u91cf\u8ba1\u7b97\u65f6\uff0c\u8003\u8651\u5b9e\u73b0\u9002\u5f53\u7684\u7f13\u5b58\u673a\u5236\uff0c\u4ee5\u9632\u6b62\u8ba1\u7b97\u76f8\u540c\u6570\u503c\u7684\u5e73\u65b9\u6839\u591a\u6b21\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">let sqrtCache = {};<\/p>\n<p>function cachedSquareRoot(value) {<\/p>\n<p>    if (sqrtCache[value]) {<\/p>\n<p>        return sqrtCache[value];<\/p>\n<p>    }<\/p>\n<p>    let result = Math.sqrt(value);<\/p>\n<p>    sqrtCache[value] = result;<\/p>\n<p>    return result;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u7bc7\u6587\u7ae0\uff0c\u60a8\u5e94\u8be5\u5bf9\u5728 JavaScript \u9879\u76ee\u7a0b\u5e8f\u4e2d\u4f7f\u7528 <code>Math.sqrt<\/code> \u65b9\u6cd5\u6709\u4e86\u6df1\u5165\u7684\u7406\u89e3\uff0c\u5e76\u80fd\u591f\u5e94\u5bf9\u53ef\u80fd\u9047\u5230\u7684\u5404\u79cd\u95ee\u9898\u3002\u8bb0\u4f4f\uff1a\u5728\u4f7f\u7528\u524d\u8fdb\u884c\u8f93\u5165\u503c\u68c0\u67e5\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5\u548c\u4f18\u5316\u6027\u80fd\u662f\u4efb\u4f55\u5f00\u53d1\u8005\u5728\u5b9e\u73b0\u6570\u5b66\u65b9\u6cd5\u65f6\u5e94\u8be5\u8003\u8651\u7684\u91cd\u70b9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u9879\u76ee\u4e2d\u4f7f\u7528 sqrt \u65b9\u6cd5\uff1f<\/strong><\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u5728 JavaScript \u4e2d\uff0c\u8981\u4f7f\u7528 sqrt \u65b9\u6cd5\u6765\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\uff0c\u4f60\u9700\u8981\u8c03\u7528 Math \u5bf9\u8c61\u7684 sqrt() \u51fd\u6570\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u8ba1\u7b97 4 \u7684\u5e73\u65b9\u6839\uff1a<\/li>\n<\/ul>\n<pre><code>let result = Math.sqrt(4);\nconsole.log(result); \/\/ \u8f93\u51fa 2\n<\/code><\/pre>\n<ul>\n<li>\u5982\u679c\u4f60\u9700\u8981\u8ba1\u7b97\u4e00\u4e2a\u53d8\u91cf\u7684\u5e73\u65b9\u6839\uff0c\u53ef\u4ee5\u5c06\u8be5\u53d8\u91cf\u4f5c\u4e3a sqrt() \u51fd\u6570\u7684\u53c2\u6570\u4f20\u5165\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a num \u7684\u53d8\u91cf\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8ba1\u7b97 num \u7684\u5e73\u65b9\u6839\uff1a<\/li>\n<\/ul>\n<pre><code>let num = 9;\nlet result = Math.sqrt(num);\nconsole.log(result); \/\/ \u8f93\u51fa 3\n<\/code><\/pre>\n<ul>\n<li>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0csqrt() \u65b9\u6cd5\u53ea\u80fd\u8ba1\u7b97\u975e\u8d1f\u6570\u7684\u5e73\u65b9\u6839\u3002\u5982\u679c\u4f20\u5165\u8d1f\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u4f1a\u8fd4\u56de NaN\uff08\u975e\u6570\u5b57\uff09\u3002\u6240\u4ee5\uff0c\u5728\u4f7f\u7528 sqrt() \u65b9\u6cd5\u65f6\uff0c\u52a1\u5fc5\u68c0\u67e5\u4f20\u5165\u7684\u53c2\u6570\u503c\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u9519\u8bef\u3002<\/li>\n<\/ul>\n<p><strong>\u600e\u4e48\u5728 JavaScript \u9879\u76ee\u4e2d\u4f7f\u7528 sqrt \u65b9\u6cd5\u8fdb\u884c\u6570\u503c\u8fd0\u7b97\uff1f<\/strong><\/p>\n<ul>\n<li>\n<p>\u5728 JavaScript \u4e2d\uff0csqrt \u65b9\u6cd5\u662f\u7528\u4e8e\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\u7684\u6570\u5b66\u51fd\u6570\u3002\u5f53\u9700\u8981\u5728\u9879\u76ee\u4e2d\u8fdb\u884c\u6570\u503c\u8fd0\u7b97\u65f6\uff0c\u53ef\u4ee5\u5229\u7528 sqrt \u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\uff0c\u5e76\u5c06\u7ed3\u679c\u4e0e\u53e6\u4e00\u4e2a\u6570\u76f8\u4e58\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre><code>let num1 = 16;\nlet num2 = 2;\nlet result = Math.sqrt(num1) * num2;\nconsole.log(result); \/\/ \u8f93\u51fa 8\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5148\u4f7f\u7528 sqrt \u65b9\u6cd5\u8ba1\u7b97 num1 \u7684\u5e73\u65b9\u6839\uff08\u8fd9\u91cc\u4e3a 4\uff09\uff0c\u7136\u540e\u5c06\u5176\u4e58\u4ee5 num2 \u7684\u503c\uff0c\u5f97\u5230\u6700\u7ec8\u7ed3\u679c 8\u3002<\/p>\n<ul>\n<li>\u9664\u4e86\u4e58\u6cd5\u8fd0\u7b97\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528 sqrt \u65b9\u6cd5\u8fdb\u884c\u5176\u4ed6\u5404\u79cd\u6570\u503c\u8fd0\u7b97\uff0c\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u548c\u9664\u6cd5\u3002\u5c06 sqrt \u65b9\u6cd5\u4e0e\u5176\u4ed6\u6570\u5b66\u8fd0\u7b97\u7b26\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u7684\u6570\u503c\u8fd0\u7b97\u903b\u8f91\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u5728 JavaScript \u9879\u76ee\u4e2d\u4f7f\u7528 sqrt \u65b9\u6cd5\u6765\u5904\u7406\u6d6e\u70b9\u6570\uff1f<\/strong><\/p>\n<ul>\n<li>\n<p>JavaScript \u7684 sqrt \u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u6574\u6570\u7684\u5e73\u65b9\u6839\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u6d6e\u70b9\u6570\u3002\u5f53\u9700\u8981\u5728\u9879\u76ee\u4e2d\u5904\u7406\u6d6e\u70b9\u6570\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 sqrt \u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u60f3\u8981\u8ba1\u7b97\u4e00\u4e2a\u6d6e\u70b9\u6570\u7684\u5e73\u65b9\u6839\uff0c\u5e76\u5c06\u7ed3\u679c\u56db\u820d\u4e94\u5165\u5230\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre><code>let num = 10.5;\nlet result = Math.sqrt(num).toFixed(2);\nconsole.log(result); \/\/ \u8f93\u51fa 3.24\n<\/code><\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 sqrt \u65b9\u6cd5\u8ba1\u7b97 num \u7684\u5e73\u65b9\u6839\uff0c\u7136\u540e\u4f7f\u7528 toFixed \u65b9\u6cd5\u5c06\u7ed3\u679c\u56db\u820d\u4e94\u5165\u5230\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\u3002<\/p>\n<ul>\n<li>\u6ce8\u610f\uff0ctoFixed \u65b9\u6cd5\u4f1a\u5c06\u7ed3\u679c\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u5982\u679c\u9700\u8981\u5c06\u7ed3\u679c\u4f5c\u4e3a\u6570\u5b57\u8fdb\u884c\u540e\u7eed\u8fd0\u7b97\uff0c\u53ef\u4ee5\u4f7f\u7528 parseFloat \u6216 Number \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre><code>let num = 10.5;\nlet result = parseFloat(Math.sqrt(num).toFixed(2));\nconsole.log(result); \/\/ \u8f93\u51fa 3.24\uff08\u4f5c\u4e3a\u6570\u5b57\u7c7b\u578b\uff09\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"JavaScript \u4e2d\u7684 Math.sqrt \u65b9\u6cd5\u7528\u4e8e\u8ba1\u7b97\u53c2\u6570\u7684\u5e73\u65b9\u6839\u3002\u7b80\u5355\u5730\u8bf4\uff0c\u5982\u679c\u8981\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u5e73\u65b9\u6839\uff0c [&hellip;]","protected":false},"author":3,"featured_media":164649,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/164645"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=164645"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/164645\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/164649"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=164645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=164645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=164645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}