{"id":310595,"date":"2024-05-20T21:54:17","date_gmt":"2024-05-20T13:54:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/310595.html"},"modified":"2024-05-20T21:54:35","modified_gmt":"2024-05-20T13:54:35","slug":"%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8-java-%e4%b8%ad%e7%9a%84-bigdecimal","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/310595.html","title":{"rendered":"\u600e\u4e48\u4f7f\u7528 Java \u4e2d\u7684 BigDecimal"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26231136\/3ceb4d84-3b41-4311-a454-3ef41276b0f2.webp\" alt=\"\u600e\u4e48\u4f7f\u7528 Java \u4e2d\u7684 BigDecimal\" \/><\/p>\n<p><p>\u4f7f\u7528Java\u4e2d\u7684<code>BigDecimal<\/code>\u662f\u5904\u7406\u9ad8\u7cbe\u5ea6\u6d6e\u70b9\u6570\u8ba1\u7b97\u7684\u6709\u6548\u65b9\u6cd5\u3002<strong><code>BigDecimal<\/code>\u63d0\u4f9b\u4e86\u7cbe\u786e\u7684\u5c0f\u6570\u8fd0\u7b97\u80fd\u529b\u3001\u65b9\u4fbf\u7684\u6570\u503c\u6bd4\u8f83\u65b9\u6cd5\u3001\u6613\u4e8e\u7406\u89e3\u7684\u6570\u5b57\u683c\u5f0f\u5316\u529f\u80fd<\/strong>\u3001\u4ee5\u53ca\u4e30\u5bcc\u7684\u6570\u5b66\u8fd0\u7b97\u652f\u6301\uff0c\u8fd9\u4e9b\u7279\u6027\u4f7f<code>BigDecimal<\/code>\u6210\u4e3a\u5904\u7406\u91d1\u878d\u3001\u79d1\u5b66\u7b49\u9886\u57df\u8ba1\u7b97\u7684\u9996\u9009\u3002\u5176\u4e2d\u6700\u5f15\u4eba\u6ce8\u76ee\u7684\u662f<strong>\u63d0\u4f9b\u4e86\u7cbe\u786e\u7684\u5c0f\u6570\u8fd0\u7b97\u80fd\u529b<\/strong>\u3002\u4e0d\u50cf<code>float<\/code>\u548c<code>double<\/code>\u7c7b\u578b\uff0c\u4f7f\u7528<code>BigDecimal<\/code>\u8fdb\u884c\u8ba1\u7b97\u65f6\u4e0d\u4f1a\u51fa\u73b0\u7cbe\u5ea6\u4e22\u5931\u95ee\u9898\uff0c\u8fd9\u5bf9\u4e8e\u91d1\u878d\u8ba1\u7b97\u3001\u7a0e\u52a1\u8ba1\u7b97\u7b49\u8981\u6c42\u6781\u9ad8\u7cbe\u786e\u5ea6\u7684\u573a\u5408\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u521b\u5efa <code>BigDecimal<\/code> \u5bf9\u8c61<\/h2>\n<\/p>\n<p><p>\u521b\u5efa<code>BigDecimal<\/code>\u5bf9\u8c61\u662f\u4f7f\u7528\u8be5\u7c7b\u7684\u7b2c\u4e00\u6b65\u3002<code>BigDecimal<\/code>\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u521b\u5efa\u5b9e\u4f8b\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528\u5b57\u7b26\u4e32\u6784\u9020\u5668\uff1a\u63a8\u8350\u4f7f\u7528\u5b57\u7b26\u4e32\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u521b\u5efa<code>BigDecimal<\/code>\uff0c\u56e0\u4e3a\u901a\u8fc7\u5b57\u7b26\u4e32\u6784\u9020\u7684\u65b9\u5f0f\u53ef\u4ee5\u5b8c\u5168\u907f\u514d\u7cbe\u5ea6\u4e22\u5931\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff1a<code>BigDecimal num = new BigDecimal(&quot;9.99&quot;);<\/code><\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4ece\u539f\u59cb\u6570\u503c\u7c7b\u578b\u6784\u9020\uff1a\u4e5f\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>double<\/code>\u6216<code>float<\/code>\u7c7b\u578b\u7684\u6570\u636e\u521b\u5efa<code>BigDecimal<\/code>\u5b9e\u4f8b\uff0c\u4f46\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u63a8\u8350\uff0c\u56e0\u4e3a\u5b58\u5728\u7cbe\u5ea6\u4e22\u5931\u7684\u98ce\u9669\u3002\u5982\uff1a<code>BigDecimal num = new BigDecimal(9.99);<\/code>\u8fd9\u6837\u867d\u7136\u7b80\u5355\uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u5e26\u6765\u610f\u60f3\u4e0d\u5230\u7684\u7cbe\u5ea6\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u6784\u9020<code>BigDecimal<\/code>\u5bf9\u8c61\u65f6\uff0c\u63a8\u8350\u4f7f\u7528\u5b57\u7b26\u4e32\u6784\u9020\u5668\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u5f0f\u6700\u80fd\u4fdd\u8bc1\u521b\u5efa\u51fa\u7684<code>BigDecimal<\/code>\u5bf9\u8c61\u7cbe\u5ea6\u51c6\u786e\uff0c\u800c\u76f4\u63a5\u4f7f\u7528\u6570\u503c\u7c7b\u578b\u6784\u9020\u867d\u7b80\u4fbf\uff0c\u5374\u53ef\u80fd\u56e0\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u7684\u7cbe\u5ea6\u635f\u5931\u800c\u5bfc\u81f4\u6700\u7ec8\u7ed3\u679c\u4e0e\u9884\u671f\u4e0d\u7b26\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001<code>BigDecimal<\/code> \u7684\u6570\u503c\u8fd0\u7b97<\/h2>\n<\/p>\n<p><p><code>BigDecimal<\/code>\u7c7b\u63d0\u4f9b\u4e86\u4e00\u5957\u5b8c\u6574\u7684\u6570\u503c\u8fd0\u7b97\u65b9\u6cd5\uff0c\u5305\u62ec\u52a0\u51cf\u4e58\u9664\u4ee5\u53ca\u6c42\u4f59\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u52a0\u6cd5\u8fd0\u7b97\uff1a\u53ef\u4ee5\u4f7f\u7528<code>add<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u52a0\u6cd5\u8fd0\u7b97\u3002\u4f8b\u5982<code>BigDecimal result = num1.add(num2);<\/code>\u5176\u4e2d<code>num1<\/code>\u548c<code>num2<\/code>\u662f\u4e24\u4e2a<code>BigDecimal<\/code>\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u51cf\u6cd5\u8fd0\u7b97\uff1a\u51cf\u6cd5\u8fd0\u7b97\u901a\u8fc7<code>subtract<\/code>\u65b9\u6cd5\u5b9e\u73b0\u3002\u6bd4\u5982<code>BigDecimal result = num1.subtract(num2);<\/code>\u8868\u793a<code>num1<\/code>\u51cf\u53bb<code>num2<\/code>\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u5728\u8fdb\u884c\u6570\u503c\u8fd0\u7b97\u65f6\uff0c\u6240\u6709\u8fd0\u7b97\u65b9\u6cd5\u90fd\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684<code>BigDecimal<\/code>\u5bf9\u8c61\uff0c\u539f\u59cb\u7684<code>BigDecimal<\/code>\u5bf9\u8c61\u5185\u5bb9\u4e0d\u4f1a\u53d1\u751f\u53d8\u5316\u3002\u8fd9\u7b26\u5408\u4e0d\u53ef\u53d8\u6a21\u5f0f\uff08Immutable Pattern\uff09\uff0c\u6709\u52a9\u4e8e\u7ef4\u62a4\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u5b89\u5168\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u6bd4\u8f83 <code>BigDecimal<\/code> \u6570\u503c<\/h2>\n<\/p>\n<p><p>\u6bd4\u8f83<code>BigDecimal<\/code>\u5bf9\u8c61\u65f6\uff0c\u4e0d\u5e94\u76f4\u63a5\u4f7f\u7528<code>equals<\/code>\u65b9\u6cd5\uff0c\u800c\u5e94\u4f7f\u7528<code>compareTo<\/code>\u65b9\u6cd5\u3002\u56e0\u4e3a<code>equals<\/code>\u4e0d\u4ec5\u6bd4\u8f83\u503c\u7684\u5927\u5c0f\uff0c\u8fd8\u4f1a\u6bd4\u8f83\u5bf9\u8c61\u7684<code>scale<\/code>\uff08\u5373\u5c0f\u6570\u70b9\u540e\u6570\u5b57\u7684\u4f4d\u6570\uff09\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>compareTo<\/code>\u8fdb\u884c\u6bd4\u8f83\uff1a<code>if (num1.compareTo(num2) &gt; 0)<\/code>\u610f\u5473\u7740<code>num1<\/code>\u5927\u4e8e<code>num2<\/code>\u3002<\/li>\n<\/ul>\n<p><p>\u5728\u4f7f\u7528<code>compareTo<\/code>\u65b9\u6cd5\u65f6\uff0c\u8fd4\u56de\u503c\u4e3a<code>int<\/code>\u7c7b\u578b\uff0c\u5927\u4e8e0\u8868\u793a\u524d\u4e00\u4e2a\u6570\u5927\u4e8e\u540e\u4e00\u4e2a\u6570\uff0c\u5c0f\u4e8e0\u8868\u793a\u524d\u4e00\u4e2a\u6570\u5c0f\u4e8e\u540e\u4e00\u4e2a\u6570\uff0c\u7b49\u4e8e0\u5219\u4e24\u6570\u76f8\u7b49\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u9002\u5408\u4e8e\u7cbe\u786e\u7684\u6570\u5b57\u5927\u5c0f\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u6570\u5b57\u683c\u5f0f\u5316<\/h2>\n<\/p>\n<p><p><code>BigDecimal<\/code>\u4e5f\u652f\u6301\u5c06\u6570\u5b57\u6309\u7167\u7279\u5b9a\u7684\u683c\u5f0f\u8fdb\u884c\u8f93\u51fa\uff0c\u8fd9\u6709\u52a9\u4e8e\u5c06\u6570\u5b57\u4ee5\u66f4\u6613\u8bfb\u7684\u65b9\u5f0f\u5c55\u73b0\u7ed9\u7528\u6237\u3002<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>DecimalFormat<\/code>\u7c7b\uff1a\u53ef\u5b9e\u73b0\u590d\u6742\u7684\u6570\u5b57\u683c\u5f0f\u5316\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u6570\u503c\u683c\u5f0f\u5316\u4e3a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u7684\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<p><p>\u683c\u5f0f\u5316\u6570\u5b57\u4e3b\u8981\u7528\u9014\u5728\u4e8e\u5c55\u793a\uff0c\u5c24\u5176\u5728\u91d1\u878d\u3001\u5546\u4e1a\u8ba1\u7b97\u7b49\u9886\u57df\uff0c\u5408\u7406\u7684\u6570\u5b57\u5c55\u793a\u80fd\u6781\u5927\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001<code>BigDecimal<\/code> \u7684\u6570\u5b66\u8fd0\u7b97<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u8fd0\u7b97\uff0c<code>BigDecimal<\/code>\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7528\u4e8e\u6570\u5b66\u8ba1\u7b97\u7684\u6269\u5c55\u65b9\u6cd5\uff0c\u5982\u6c42\u7edd\u5bf9\u503c\u3001\u6c42\u5e42\u3001\u7b97\u672f\u8fd0\u7b97\u7684\u7cbe\u786e\u63a7\u5236\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u6c42\u5e42\u8fd0\u7b97\uff1a\u901a\u8fc7<code>pow<\/code>\u65b9\u6cd5\u5b9e\u73b0\u3002<code>BigDecimal result = num.pow(2);<\/code>\u8868\u793a<code>num<\/code>\u76842\u6b21\u65b9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u7f6e\u7cbe\u5ea6\uff1a\u5728\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u65f6\uff0c<code>BigDecimal<\/code>\u5141\u8bb8\u8bbe\u7f6e\u7ed3\u679c\u7684\u7cbe\u5ea6\u53ca\u820d\u5165\u6a21\u5f0f\uff0c\u8fd9\u5bf9\u4e8e\u91d1\u878d\u8ba1\u7b97\u7b49\u9700\u8981\u9ad8\u5ea6\u7cbe\u786e\u63a7\u5236\u7684\u573a\u5408\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p>\u5728\u5b9e\u9645\u7f16\u7a0b\u4e2d\uff0c\u4e86\u89e3\u5e76\u5408\u7406\u5229\u7528<code>BigDecimal<\/code>\u7684\u8fd9\u4e9b\u529f\u80fd\uff0c\u80fd\u6709\u6548\u5730\u5904\u7406\u548c\u907f\u514d\u6d6e\u70b9\u6570\u8ba1\u7b97\u4e2d\u7684\u7cbe\u5ea6\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u7684\u5e94\u7528\u573a\u666f\u4e0b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><em><strong>1. \u5feb\u901f\u4e0a\u624b Java \u4e2d\u7684 BigDecimal \u662f\u4ec0\u4e48\uff1f<\/strong><\/em><\/p>\n<p>BigDecimal \u662f Java \u4e2d\u7528\u6765\u5904\u7406\u9ad8\u7cbe\u5ea6\u6570\u5b57\u8ba1\u7b97\u7684\u7c7b\u3002\u5b83\u53ef\u4ee5\u5b9e\u73b0\u4efb\u610f\u4f4d\u6570\u7684\u5c0f\u6570\u70b9\u8ba1\u7b97\uff0c\u907f\u514d\u4e86\u6d6e\u70b9\u6570\u8fd0\u7b97\u8fc7\u7a0b\u4e2d\u7684\u7cbe\u5ea6\u4e22\u5931\u95ee\u9898\u3002\u4f7f\u7528 BigDecimal \u53ef\u4ee5\u63d0\u4f9b\u66f4\u9ad8\u7684\u6570\u503c\u8ba1\u7b97\u51c6\u786e\u6027\u3002<\/p>\n<p><em><strong>2. \u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 BigDecimal \u5b9e\u4f8b\uff1f<\/strong><\/em><\/p>\n<p>\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 BigDecimal \u5b9e\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528 BigDecimal \u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u6216\u8005\u4e00\u4e2a\u6570\u5b57\u5b9e\u4f8b\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">BigDecimal num1 = new BigDecimal(&quot;0.1&quot;); \/\/ \u4f7f\u7528\u5b57\u7b26\u4e32\u521b\u5efa BigDecimal \u5bf9\u8c61\nBigDecimal num2 = new BigDecimal(0.1); \/\/ \u4f7f\u7528\u6570\u5b57\u521b\u5efa BigDecimal \u5bf9\u8c61\n<\/code><\/pre>\n<p><em><strong>3. \u5982\u4f55\u8fdb\u884c BigDecimal \u7684\u52a0\u51cf\u4e58\u9664\u64cd\u4f5c\uff1f<\/strong><\/em><\/p>\n<p>BigDecimal \u901a\u8fc7\u65b9\u6cd5\u8c03\u7528\u53ef\u4ee5\u8fdb\u884c\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u7b49\u6570\u503c\u8fd0\u7b97\u64cd\u4f5c\u3002\u8fd9\u4e9b\u65b9\u6cd5\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u65b0\u7684 BigDecimal \u5b9e\u4f8b\uff0c\u539f\u6765\u7684\u5b9e\u4f8b\u5e76\u4e0d\u4f1a\u53d1\u751f\u6539\u53d8\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">BigDecimal num1 = new BigDecimal(&quot;10.5&quot;);\nBigDecimal num2 = new BigDecimal(&quot;5.2&quot;);\n\nBigDecimal sum = num1.add(num2); \/\/ \u52a0\u6cd5\u8fd0\u7b97\nBigDecimal difference = num1.subtract(num2); \/\/ \u51cf\u6cd5\u8fd0\u7b97\nBigDecimal product = num1.multiply(num2); \/\/ \u4e58\u6cd5\u8fd0\u7b97\nBigDecimal quotient = num1.divide(num2, 2, RoundingMode.HALF_UP); \/\/ \u9664\u6cd5\u8fd0\u7b97\uff0c\u7ed3\u679c\u4fdd\u75592\u4f4d\u5c0f\u6570\uff0c\u56db\u820d\u4e94\u5165\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Java\u4e2d\u7684BigDecimal\u662f\u5904\u7406\u9ad8\u7cbe\u5ea6\u6d6e\u70b9\u6570\u8ba1\u7b97\u7684\u6709\u6548\u65b9\u6cd5\u3002BigDecimal\u63d0\u4f9b\u4e86\u7cbe\u786e\u7684\u5c0f\u6570\u8fd0\u7b97 [&hellip;]","protected":false},"author":3,"featured_media":310621,"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\/310595"}],"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=310595"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/310595\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/310621"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=310595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=310595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=310595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}