{"id":185,"date":"2025-06-29T19:59:44","date_gmt":"2025-06-29T11:59:44","guid":{"rendered":"https:\/\/www.rbfwq.com\/blog\/?p=185"},"modified":"2025-06-29T19:59:44","modified_gmt":"2025-06-29T11:59:44","slug":"%e5%a6%82%e4%bd%95%e7%b2%be%e5%87%86%e6%a0%bc%e5%bc%8f%e5%8c%96bigdecimal%e5%b9%b6%e4%bf%9d%e7%95%99%e4%b8%a4%e4%bd%8d%e5%b0%8f%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.idc61.com\/blog\/185.html","title":{"rendered":"\u5982\u4f55\u7cbe\u51c6\u683c\u5f0f\u5316BigDecimal\u5e76\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570"},"content":{"rendered":"<p><p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idc61.com\/blog\/wp-content\/uploads\/2025\/06\/no7y5wJ.jpg\" alt=\"\u5982\u4f55\u7cbe\u51c6\u683c\u5f0f\u5316BigDecimal\u5e76\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\" title=\"\u5982\u4f55\u7cbe\u51c6\u683c\u5f0f\u5316BigDecimal\u5e76\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\"><\/p>\n<p><h3>1. \u683c\u5f0f\u5316 BigDecimal \u4fdd\u7559\u540e\u4e24\u4f4d\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p>\u8981\u683c\u5f0f\u5316 BigDecimal \u6570\u636e\u5e76\u4fdd\u7559\u540e\u4e24\u4f4d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Java \u7684 DecimalFormat \u7c7b\u6216 BigDecimal \u63d0\u4f9b\u7684\u65b9\u6cd5\u3002\u8fd9\u6837\u505a\u80fd\u591f\u786e\u4fdd\u6570\u503c\u5728\u663e\u793a\u548c\u8ba1\u7b97\u65f6\u7684\u4e00\u81f4\u6027\u3002BigDecimal \u63d0\u4f9b\u4e86\u4e00\u4e2a\u540d\u4e3a setScale \u7684\u65b9\u6cd5\uff0c\u8ba9\u4f60\u53ef\u4ee5\u8f7b\u677e\u8bbe\u7f6e\u5c0f\u6570\u4f4d\u6570\uff0c\u540c\u65f6\u652f\u6301\u820d\u5165\u6a21\u5f0f\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u6570\u503c\u56db\u820d\u4e94\u5165\u5230\u5c0f\u6570\u70b9\u540e\u4e24\u4f4d\uff0c\u8fd9\u5728\u8d22\u52a1\u8ba1\u7b97\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p>\n<h3>2. \u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4<\/h3>\n<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 BigDecimal \u7684 setScale \u65b9\u6cd5\u7684\u5177\u4f53\u6b65\u9aa4\u3002\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a BigDecimal \u5b9e\u4f8b\uff0c\u7136\u540e\u8c03\u7528 setScale \u65b9\u6cd5\uff0c\u4f20\u5165\u9700\u8981\u7684\u5c0f\u6570\u4f4d\u6570\u548c\u820d\u5165\u6a21\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code>import java.math.BigDecimal;  <\/p>\r\n<p>import java.math.RoundingMode;  <\/p>\r\n\r\n<p>public class Main {  <\/p>\r\n<p>    public static void main(String[] args) {  <\/p>\r\n<p>        BigDecimal value = new BigDecimal(\"123.4567\");  <\/p>\r\n<p>        BigDecimal formattedValue = value.setScale(2, RoundingMode.HALF_UP);  <\/p>\r\n<p>        System.out.println(formattedValue);  \/\/ \u8f93\u51fa\uff1a123.46  <\/p>\r\n<p>    }  <\/p>\r\n<p>}  <\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\n<h3>3. DecimalFormat \u7684\u4f7f\u7528\u65b9\u6cd5<\/h3>\n<\/p>\n<p>\u53e6\u4e00\u79cd\u683c\u5f0f\u5316 BigDecimal \u7684\u65b9\u6cd5\u662f\u4f7f\u7528 DecimalFormat \u7c7b\u3002DecimalFormat \u53ef\u4ee5\u8ba9\u4f60\u66f4\u7075\u6d3b\u5730\u5904\u7406\u683c\u5f0f\u5316\u8f93\u51fa\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b9e\u4f8b\u5316 DecimalFormat \u5bf9\u8c61\uff0c\u5e76\u5b9a\u4e49\u6a21\u5f0f\u5b57\u7b26\u4e32\u3002\u6a21\u5f0f\u5b57\u7b26\u4e32\u4e2d\uff0c&#8221;0.00&#8243; \u8868\u793a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 DecimalFormat \u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code>import java.math.BigDecimal;  <\/p>\r\n<p>import java.text.DecimalFormat;  <\/p>\r\n\r\n<p>public class Main {  <\/p>\r\n<p>    public static void main(String[] args) {  <\/p>\r\n<p>        BigDecimal value = new BigDecimal(\"123.4567\");  <\/p>\r\n<p>        DecimalFormat df = new DecimalFormat(\"0.00\");  <\/p>\r\n<p>        String formattedValue = df.format(value);  <\/p>\r\n<p>        System.out.println(formattedValue);  \/\/ \u8f93\u51fa\uff1a123.46  <\/p>\r\n<p>    }  <\/p>\r\n<p>}  <\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p>\n<h3>4. \u4fdd\u7559\u540e\u4e24\u4f4d\u7684\u573a\u666f<\/h3>\n<\/p>\n<p>\u5728\u91d1\u878d\u548c\u5546\u4e1a\u9886\u57df\u8fdb\u884c\u8ba1\u7b97\u65f6\uff0c\u4fdd\u7559\u5c0f\u6570\u4f4d\u6570\u662f\u4e00\u9879\u57fa\u672c\u8981\u6c42\uff0c\u5c24\u5176\u662f\u5728\u8d27\u5e01\u8ba1\u7b97\u4e2d\u3002BigDecimal \u662f\u5904\u7406\u8fd9\u4e9b\u8ba1\u7b97\u7684\u6700\u4f73\u9009\u62e9\uff0c\u5b83\u63d0\u4f9b\u7cbe\u786e\u7684\u6d6e\u70b9\u8fd0\u7b97\uff0c\u907f\u514d\u4e86\u4f7f\u7528 double \u6216 float \u53ef\u80fd\u5e26\u6765\u7684\u7cbe\u5ea6\u635f\u5931\u3002<\/p>\n<\/p>\n<p>\n<h3>5. \u6211\u8be5\u9009\u62e9\u54ea\u4e2a\u65b9\u6cd5\uff1f<\/h3>\n<\/p>\n<p><b>\u4f7f\u7528 BigDecimal \u7684 setScale \u65b9\u6cd5\uff0c\u8fd8\u662f\u9009\u62e9 DecimalFormat\uff1f<\/b>  <\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u4e3b\u8981\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u3002\u5982\u679c\u4f60\u9700\u8981\u8fdb\u884c\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u5e76\u8981\u4fdd\u7559\u6570\u503c\u7684\u672c\u8d28\uff0c\u4f7f\u7528 BigDecimal \u662f\u6bd4\u8f83\u597d\u7684\u9009\u62e9\u3002\u800c\u5982\u679c\u4f60\u53ea\u662f\u60f3\u8f93\u51fa\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\uff0cDecimalFormat \u5219\u66f4\u52a0\u65b9\u4fbf\u3002\u4e24\u8005\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u573a\u666f\u5408\u7406\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p>\n<h3>6. BigDecimal \u7684\u6027\u80fd\u5982\u4f55\uff1f<\/h3>\n<\/p>\n<p><b>BigDecimal \u5728\u6027\u80fd\u4e0a\u4f1a\u6bd4\u57fa\u672c\u7c7b\u578b\u6162\u5417\uff1f<\/b>  <\/p>\n<p>\u662f\u7684\uff0cBigDecimal \u7684\u8ba1\u7b97\u6027\u80fd\u76f8\u5bf9\u8f83\u6162\uff0c\u56e0\u4e3a\u5b83\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6d89\u53ca\u5230\u66f4\u591a\u7684\u5185\u5b58\u7ba1\u7406\u548c\u5bf9\u8c61\u521b\u5efa\u3002\u4f46\u662f\uff0c\u5728\u9700\u8981\u9ad8\u5ea6\u7cbe\u786e\u7684\u91d1\u878d\u8ba1\u7b97\u65f6\uff0c\u5176\u4f18\u52bf\u660e\u663e\uff0c\u65e0\u6cd5\u7528\u6027\u80fd\u8fdb\u884c\u4ea4\u6362\u3002\u56e0\u6b64\uff0c\u5f53\u7cbe\u5ea6\u662f\u9996\u8981\u8003\u8651\u65f6\uff0cBigDecimal \u662f\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<p>\n<h3>7. \u5982\u4f55\u5904\u7406\u56db\u820d\u4e94\u5165\u7684\u4e0d\u540c\u9700\u6c42\uff1f<\/h3>\n<\/p>\n<p><b>\u6211\u7684\u5e94\u7528\u9700\u8981\u4e0d\u540c\u7684\u820d\u5165\u65b9\u5f0f\uff0cbigDecimal \u80fd\u652f\u6301\u5417\uff1f<\/b>  <\/p>\n<p>\u5f53\u7136\u53ef\u4ee5\uff0cBigDecimal \u63d0\u4f9b\u4e86\u591a\u79cd\u820d\u5165\u6a21\u5f0f\uff0c\u5305\u62ec HALF_UP\u3001HALF_DOWN\u3001CEILING\u3001FLOOR \u7b49\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u7075\u6d3b\u8c03\u6574\u820d\u5165\u65b9\u5f0f\u3002\u5728 setScale \u65b9\u6cd5\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f20\u5165\u4e0d\u540c\u7684 RoundingMode \u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u9700\u8981\u5411\u4e0b\u53d6\u6574\uff0c\u53ef\u4ee5\u4f7f\u7528 RoundingMode.FLOOR\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cBigDecimal \u548c DecimalFormat \u7ed3\u5408\u4f7f\u7528\u53ef\u4ee5\u89e3\u51b3\u5927\u591a\u6570\u9700\u8981\u5c0f\u6570\u4f4d\u6570\u683c\u5f0f\u5316\u7684\u573a\u666f\u3002\u65e0\u8bba\u662f\u5728\u8ba1\u7b97\u8fd8\u662f\u5728\u8f93\u51fa\u65f6\u4fdd\u6301\u6570\u503c\u7684\u51c6\u786e\u6027\uff0c\u8fd9\u4e24\u79cd\u65b9\u6848\u90fd\u80fd\u5f88\u597d\u7684\u6ee1\u8db3\u9700\u6c42\u3002\u8bb0\u4f4f\u9009\u62e9\u6700\u80fd\u9002\u5e94\u4f60\u4e1a\u52a1\u9700\u6c42\u7684\u65b9\u6cd5\uff0c\u5e76\u5408\u7406\u7ba1\u7406\u6027\u80fd\u548c\u7cbe\u5ea6\u7684\u5e73\u8861\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u683c\u5f0f\u5316 BigDecimal \u4fdd\u7559\u540e\u4e24\u4f4d\u7684\u65b9\u6cd5 \u8981\u683c\u5f0f\u5316 BigDecimal \u6570\u636e\u5e76\u4fdd\u7559\u540e\u4e24\u4f4d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Java \u7684 DecimalFormat \u7c7b\u6216 BigDecimal \u63d0\u4f9b\u7684\u65b9\u6cd5\u3002\u8fd9\u6837\u505a\u80fd\u591f\u786e\u4fdd\u6570\u503c\u5728\u663e\u793a\u548c\u8ba1\u7b97\u65f6\u7684\u4e00\u81f4\u6027&#8230;<\/p>\n","protected":false},"author":1,"featured_media":186,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[43,44,45],"topic":[],"class_list":["post-185","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jishu","tag-bigdecimal","tag-setscale","tag-45"],"_links":{"self":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/comments?post=185"}],"version-history":[{"count":1,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":189,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/posts\/185\/revisions\/189"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/media\/186"}],"wp:attachment":[{"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/media?parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/categories?post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/tags?post=185"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idc61.com\/blog\/wp-json\/wp\/v2\/topic?post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}