{"id":104,"date":"2023-02-18T08:08:43","date_gmt":"2023-02-18T00:08:43","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=104"},"modified":"2023-02-18T08:09:43","modified_gmt":"2023-02-18T00:09:43","slug":"bigdecimal-scale","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/bigdecimal-scale\/","title":{"rendered":"BigDecimal\u4fdd\u7559\u5c0f\u6570\u4f4d"},"content":{"rendered":"<pre><code class=\"language-java\">public class BigDecimalFormat {\n\n    public static void main(String[] args) {\n        BigDecimal decimal = new BigDecimal(&quot;1.12345&quot;);\n        System.out.println(decimal);\n        BigDecimal scale = decimal.setScale(4, BigDecimal.ROUND_HALF_DOWN);\n        System.out.println(scale);\n\n        BigDecimal scale2 = decimal.setScale(4, BigDecimal.ROUND_HALF_UP);\n        System.out.println(scale2);\n    }\n}<\/code><\/pre>\n<p><code>BigDecimal.setScale()<\/code>\u65b9\u6cd5\u7528\u4e8e\u683c\u5f0f\u5316\u5c0f\u6570\u70b9<\/p>\n<pre><code class=\"language-java\">setScale(1) \u8868\u793a\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\uff0c\u9ed8\u8ba4\u7528\u56db\u820d\u4e94\u5165\u65b9\u5f0f\nsetScale(1, BigDecimal.ROUND_DOWN) \u76f4\u63a5\u5220\u9664\u591a\u4f59\u7684\u5c0f\u6570\u4f4d\uff0c\u59822.35\u4f1a\u53d8\u62102.3\nsetScale(1, BigDecimal.ROUND_UP) \u8fdb\u4f4d\u5904\u7406\uff0c2.35\u53d8\u62102.4\nsetScale(1, BigDecimal.ROUND_HALF_UP) \u56db\u820d\u4e94\u5165\uff0c2.35\u53d8\u62102.4\nsetScale(1, BigDecimal.ROUND_HALF_DOWN) \u4e94\u820d\u516d\u5165\uff0c2.35\u53d8\u62102.3<\/code><\/pre>\n<p>\u53c2\u6570\u5b9a\u4e49<\/p>\n<ul>\n<li><strong>ROUND_CEILING<\/strong><\/li>\n<\/ul>\n<p>Rounding mode to round towards positive infinity.<br \/>\n\u5411\u6b63\u65e0\u7a77\u65b9\u5411\u820d\u5165<\/p>\n<ul>\n<li><strong>ROUND_DOWN<\/strong><\/li>\n<\/ul>\n<p>Rounding mode to round towards zero.<br \/>\n\u5411\u96f6\u65b9\u5411\u820d\u5165 <\/p>\n<ul>\n<li><strong>ROUND_FLOOR<\/strong><\/li>\n<\/ul>\n<p>Rounding mode to round towards negative infinity.<br \/>\n\u5411\u8d1f\u65e0\u7a77\u65b9\u5411\u820d\u5165 <\/p>\n<ul>\n<li><strong>ROUND_HALF_DOWN<\/strong><\/li>\n<\/ul>\n<p>Rounding mode to round towards &quot;nearest neighbor&quot; unless both neighbors are equidistant, in which case round down.<br \/>\n\u5411\uff08\u8ddd\u79bb\uff09\u6700\u8fd1\u7684\u4e00\u8fb9\u820d\u5165\uff0c\u9664\u975e\u4e24\u8fb9\uff08\u7684\u8ddd\u79bb\uff09\u662f\u76f8\u7b49,\u5982\u679c\u662f\u8fd9\u6837\uff0c\u5411\u4e0b\u820d\u5165, \u4f8b\u59821.55 \u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\u7ed3\u679c\u4e3a1.5 <\/p>\n<ul>\n<li><strong>ROUND_HALF_EVEN<\/strong><\/li>\n<\/ul>\n<p>Rounding mode to round towards the &quot;nearest neighbor&quot; unless both neighbors are equidistant, in which case, round towards the even neighbor.<br \/>\n\u5411\uff08\u8ddd\u79bb\uff09\u6700\u8fd1\u7684\u4e00\u8fb9\u820d\u5165\uff0c\u9664\u975e\u4e24\u8fb9\uff08\u7684\u8ddd\u79bb\uff09\u662f\u76f8\u7b49,\u5982\u679c\u662f\u8fd9\u6837\uff0c\u5982\u679c\u4fdd\u7559\u4f4d\u6570\u662f\u5947\u6570\uff0c\u4f7f\u7528ROUND_HALF_UP \uff0c\u5982\u679c\u662f\u5076\u6570\uff0c\u4f7f\u7528ROUND_HALF_DOWN <\/p>\n<ul>\n<li><strong>ROUND_HALF_UP<\/strong><\/li>\n<\/ul>\n<p>Rounding mode to round towards &quot;nearest neighbor&quot; unless both neighbors are equidistant, in which case round up.<br \/>\n\u5411\uff08\u8ddd\u79bb\uff09\u6700\u8fd1\u7684\u4e00\u8fb9\u820d\u5165\uff0c\u9664\u975e\u4e24\u8fb9\uff08\u7684\u8ddd\u79bb\uff09\u662f\u76f8\u7b49,\u5982\u679c\u662f\u8fd9\u6837\uff0c\u5411\u4e0a\u820d\u5165, 1.55\u4fdd\u7559\u4e00\u4f4d\u5c0f\u6570\u7ed3\u679c\u4e3a1.6 <\/p>\n<ul>\n<li><strong>ROUND_UNNECESSARY<\/strong><\/li>\n<\/ul>\n<p>Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.<br \/>\n\u8ba1\u7b97\u7ed3\u679c\u662f\u7cbe\u786e\u7684\uff0c\u4e0d\u9700\u8981\u820d\u5165\u6a21\u5f0f <\/p>\n<ul>\n<li><strong>ROUND_UP<\/strong><\/li>\n<\/ul>\n<p>Rounding mode to round away from zero.<br \/>\n\u5411\u8fdc\u79bb0\u7684\u65b9\u5411\u820d\u5165<\/p>\n","protected":false},"excerpt":{"rendered":"<p>public class BigDecimalFormat { public static void main [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-java-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}