{"id":3129,"date":"2024-11-10T14:20:46","date_gmt":"2024-11-10T06:20:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3129\/"},"modified":"2024-11-10T14:20:46","modified_gmt":"2024-11-10T06:20:46","slug":"js%e5%a6%82%e4%bd%95%e4%bf%9d%e7%95%99%e4%b8%a4%e4%bd%8d%e5%b0%8f%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3129\/","title":{"rendered":"js\u5982\u4f55\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 tofixed() \u65b9\u6cd5\uff08number.tofixed(2)\uff09\u6216 number() \u51fd\u6570\uff08number(numberstring).tofixed(2)\uff09\u5c06\u6570\u5b57\u4fdd\u7559\u5230\u4e24\u4f4d\u5c0f\u6570\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 slice() \u65b9\u6cd5\uff08numberstring.slice(0, numberstring.indexof(&#8220;.&#8221;) + 3)\uff09\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528 math.round()\u3001math.floor() \u6216\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110804250161324.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u63d2\u56fe\" alt=\"js\u5982\u4f55\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570<\/strong><\/p>\n<p><strong>\u56fa\u5b9a\u6570\u5b57\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 toFixed() \u65b9\u6cd5\u5c06\u6570\u5b57\u4fdd\u7559\u5230\u6307\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u5bf9\u4e8e\u4e24\u4f4d\u5c0f\u6570\uff0c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>number.toFixed(2);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>const number = 123.456;\nconst result = number.toFixed(2); \/\/ \"123.46\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 Number \u51fd\u6570\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 Number() \u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u7136\u540e\u4f7f\u7528 toFixed() \u65b9\u6cd5\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<pre>const numberString = \"123.456\";\nconst result = Number(numberString).toFixed(2); \/\/ \"123.46\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5b57\u7b26\u4e32\u64cd\u4f5c\uff1a<\/strong><\/p>\n<p>\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 slice() \u65b9\u6cd5\u622a\u53d6\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<pre>const numberString = \"123.456\";\nconst result = numberString.slice(0, numberString.indexOf(\".\") + 3); \/\/ \"123.46\"<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li> <strong>Math.round()\uff1a<\/strong>\u56db\u820d\u4e94\u5165\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\uff0c\u7136\u540e\u4f7f\u7528 toFixed() \u65b9\u6cd5\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/li>\n<li> <strong>Math.floor()\uff1a<\/strong>\u5411\u4e0b\u53d6\u6574\u5230\u6700\u63a5\u8fd1\u7684\u6574\u6570\uff0c\u7136\u540e\u4f7f\u7528 toFixed() \u65b9\u6cd5\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002<\/li>\n<li> <strong>\u81ea\u5b9e\u73b0\u51fd\u6570\uff1a<\/strong>\u521b\u5efa\u81ea\u5df1\u7684\u51fd\u6570\u6765\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\uff0c\u5982\uff1a<\/li>\n<\/ul>\n<pre>function roundToTwo(number) {\n  return (Math.round(number * 100) \/ 100).toFixed(2);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u65b9\u6cd5\u90fd\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fdd\u7559\u6570\u5b57\u5230\u4e24\u4f4d\u5c0f\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 tofixed() \u65b9\u6cd5\uff08number.tofixed(2)\uff09\u6216 number() \u51fd\u6570\uff08number(numberstring).tofixed(2)\uff09\u5c06\u6570\u5b57\u4fdd\u7559\u5230\u4e24\u4f4d\u5c0f\u6570\u3002\u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 slice() \u65b9\u6cd5\uff08numberstring.slice(0, numberstring.indexof(&#8220;.&#8221;) + 3)\uff09\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528 math.round()\u3001math.floor() \u6216\u81ea\u5b9a\u4e49\u51fd\u6570\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570 \u56fa\u5b9a\u6570\u5b57\uff1a \u4f7f\u7528 toFixed() \u65b9\u6cd5\u5c06\u6570\u5b57\u4fdd\u7559\u5230\u6307\u5b9a\u7684\u5c0f\u6570\u4f4d\u6570\u3002\u5bf9\u4e8e\u4e24\u4f4d\u5c0f\u6570\uff0c\u8bed\u6cd5\u4e3a\uff1a number.toFixed(2); \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff1a const number = 123.456; const result = number.toFixed(2); \/\/ &#8220;123.46&#8221; \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 Number \u51fd\u6570\uff1a \u4f7f\u7528 Number() \u51fd\u6570\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u6570\u5b57\uff0c\u7136\u540e\u4f7f\u7528 toFixed() \u65b9\u6cd5\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u3002 const numberString = &#8220;123.456&#8221;; const result = Number(numberString).toFixed(2); \/\/ &#8220;123.46&#8221; \u767b\u5f55\u540e\u590d\u5236 \u5b57\u7b26\u4e32\u64cd\u4f5c\uff1a \u5bf9\u4e8e\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u4f7f\u7528 slice() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3129","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3129"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3129\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}