{"id":8415,"date":"2021-03-15T17:55:53","date_gmt":"2021-03-15T09:55:53","guid":{"rendered":"https:\/\/sdeno.com\/?p=8415"},"modified":"2025-05-07T15:56:49","modified_gmt":"2025-05-07T07:56:49","slug":"%e8%a7%a3%e5%86%b3js%e8%ae%a1%e7%ae%97%e6%95%b0%e5%ad%97%e7%b2%be%e5%ba%a6%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=8415","title":{"rendered":"\u89e3\u51b3js\u8ba1\u7b97\u6570\u5b57\u7cbe\u5ea6\u95ee\u9898"},"content":{"rendered":"<p>&nbsp;<\/p>\n<pre>function twofloat(v){\r\n  return Number((Number(v).toFixed(2)).toString()) \/\/\u6709\u5c0f\u6570\u4fdd\u7559\uff0c\u6ca1\u5c0f\u6570\u5c31\u4e0d\u7528\u9ed8\u8ba4\u663e\u793a.00\uff0c\u591a\u4f4d\u5c0f\u6570\u5c31\u4fdd\u75592\u4f4d\uff0c\u540c\u65f6\u89e3\u51b3\u7cbe\u5ea6\u95ee\u9898\r\n}<\/pre>\n<pre>console.log( twofloat(0.3-0.1) )<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<\/p>\n<pre>console.log(0.1+0.2); \/\/ \u8f93\u51fa 0.30000000000000004\r\nconsole.log(0.3-0.1); \/\/ \u8f93\u51fa\uff1a0.19999999999999998\r\nconsole.log(0.07*100); \/\/ \u8f93\u51fa 7.000000000000001<\/pre>\n<p>&nbsp;<\/p>\n<p>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u89e3\u51b3\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/MikeMcl\/big.js\" target=\"_blank\" rel=\"noopener\">big.js<\/a>\u00a0\uff086kb\uff09<\/li>\n<li><a href=\"https:\/\/github.com\/MikeMcl\/bignumber.js\" target=\"_blank\" rel=\"noopener\">bignumber.js<\/a>\u00a0\uff0818.1kb\uff09<\/li>\n<li><a href=\"https:\/\/github.com\/MikeMcl\/decimal.js\" target=\"_blank\" rel=\"noopener\">decimal.js<\/a>\u00a0\uff0830.8kb\uff09<\/li>\n<li><a href=\"https:\/\/github.com\/josdejong\/mathjs\" target=\"_blank\" rel=\"noopener\">mathjs<\/a><\/li>\n<\/ul>\n<p>\u5176\u4e2d\uff0c\u524d3\u4e2a\u662f\u540c\u4e00\u4e2a\u4f5c\u8005\uff0c\u4f53\u79ef\u4e00\u4e2a\u6bd4\u4e00\u4e2a\u5927\uff0c\u53c2\u89c1\u4f5c\u8005\u5199\u7684\uff1a<a href=\"https:\/\/github.com\/MikeMcl\/big.js\/wiki\" target=\"_blank\" rel=\"noopener\">big.js\uff0cbignumber.js\u548cdecimal.js\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/a><\/p>\n<p>\u4ee5decimal.js\u4e3a\u4f8b\uff1a<\/p>\n<pre>new Decimal(0.1).plus(0.2).toString() \/\/ '0.3'\r\nnew Decimal(0.3).minus(0.1).toString() \/\/ '0.2'\r\nnew Decimal(0.07).times(100).toString() \/\/ '7'<\/pre>\n<p>\u6700\u540e\u4e00\u4e2a mathjs \u662f\u6bd4\u8f83\u51fa\u540d\u7684\u4e13\u95e8\u5904\u7406\u6570\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u529f\u80fd\u975e\u5e38\u5168\uff0c\u5982\u679c\u53ea\u662f\u7528\u6765\u89e3\u51b3\u7cbe\u5ea6\u8ba1\u7b97\u7528\u8fd9\u4e2a\u5e93\u4f1a\u663e\u5f97\u6740\u9e21\u7528\u725b\u5200\u3002<\/p>\n<figure class=\"mdx-lazyload-container\" style=\"max-width:988px\"><div style=\"padding-top:45.748987854251%\"><\/div><div class=\"mdx-img-loading-sp mdui-valign\"><div><div class=\"mdui-spinner\"><\/div><\/div><\/div><img width=\"988\" height=\"452\" class=\"alignnone size-full wp-image-8416 lazyload\" title=\"\u89e3\u51b3js\u8ba1\u7b97\u6570\u5b57\u7cbe\u5ea6\u95ee\u9898\" src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"https:\/\/sdeno.com\/wp-content\/uploads\/2021\/03\/mathjs.png\" alt=\"https:\/\/sdeno.com\/wp-content\/uploads\/2021\/03\/mathjs.png\" data-srcset=\"https:\/\/sdeno.com\/wp-content\/uploads\/2021\/03\/mathjs.png 988w, https:\/\/sdeno.com\/wp-content\/uploads\/2021\/03\/mathjs-300x137.png 300w, https:\/\/sdeno.com\/wp-content\/uploads\/2021\/03\/mathjs-768x351.png 768w\" sizes=\"auto, (max-width: 988px) 100vw, 988px\"><\/figure>\n<p><a href=\"http:\/\/blog.haoji.me\/js-float.html\" target=\"_blank\" rel=\"noopener\">http:\/\/blog.haoji.me\/js-float.html<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; function twofloat(v){ return Number((Number(v).t [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-8415","post","type-post","status-publish","format-standard","hentry","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/8415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8415"}],"version-history":[{"count":1,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/8415\/revisions"}],"predecessor-version":[{"id":9942,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/8415\/revisions\/9942"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}