{"id":135877,"date":"2020-07-21T16:13:10","date_gmt":"2020-07-21T08:13:10","guid":{"rendered":"http:\/\/4563.org\/?p=135877"},"modified":"2020-07-21T16:13:10","modified_gmt":"2020-07-21T08:13:10","slug":"%e5%b0%8f%e5%bf%83-java-math-mathcontext","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=135877","title":{"rendered":"\u5c0f\u5fc3 Java .math.MathContext"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5c0f\u5fc3 Java .math.MathContext               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : anzu <\/span>  <span><i><\/i> 13<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4f60\u4ee5\u4e3a\u4ed6\u4eec\u8f93\u51fa\u662f\u76f8\u540c\u7684\uff0c\u5176\u5b9e\u5b8c\u5168\u4e0d\u540c<\/p>\n<pre><code>BigDecimal original = new BigDecimal(\"2.345\"); BigDecimal scaled = original.add(BigDecimal.TEN).setScale(2, RoundingMode.HALF_UP); MathContext mc = new MathContext(2, RoundingMode.HALF_UP); BigDecimal scaled2 = original.add(BigDecimal.TEN, mc);  \/\/ 12.35 System.out.println(scaled.toPlainString()); \/\/ 12 System.out.println(scaled2.toPlainString()); <\/code><\/pre>\n<p>MathContext \u7684\u6784\u9020\u51fd\u6570 MathContext(int setPrecision, RoundingMode setRoundingMode) \u5176\u4e2d setPrecision \u6307\u7684\u662f\u6709\u6548\u6570\u5b57\uff0c\u800c\u4e0d\u662f\u5c0f\u6570\u4fdd\u7559\u4f4d\u6570\u3002\u4e0e roundingMode \u53c2\u6570\u653e\u4e00\u8d77\u5bb9\u6613\u4ee4\u4eba\u6df7\u6dc6\u3002\u800c\u4e14\u4e00\u822c\u7528\u5230 BigDecimal \u591a\u534a\u662f\u91cd\u8981\u7684\u8ba1\u7b97\u573a\u666f\uff0c\u4e00\u65e6\u51fa\u9519\u540e\u679c\u4e25\u91cd\u3002<\/p>\n<p>\u4e5f\u6709\u4e0d\u5c11\u4eba\u5410\u69fd\uff1a https:\/\/stackoverflow.com\/questions\/7539\/use-of-java-math-mathcontext<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2404127\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : aguesuka <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5355\u5143\u6d4b\u8bd5\u7684\u91cd\u8981\u6027\u5c31\u51fa\u6765\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2404128\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : anzu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @aguesuka \u6211\u5c31\u662f\u5199\u5355\u5143\u6d4b\u8bd5\u624d\u53d1\u73b0\u7684..                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2404129\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lululau <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             precision \u76f4\u7ffb\u4e0d\u662f\u201c\u7cbe\u5ea6\u201d\u7684\u610f\u601d\u5417\uff0c\u7cbe\u5ea6\u4e0d\u662f\u76f8\u5f53\u4e8e\u6709\u6548\u6570\u5b57\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2404130\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yidinghe <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @lululau \u5bf9\uff0c\u4e8b\u540e\u518d\u6765\u770b\u90fd\u770b\u5f97\u61c2\uff0c\u5c31\u662f\u7b2c\u4e00\u773c\u5149\u770b\u53c2\u6570\u7c7b\u578b\u7684\u8bdd\u5bb9\u6613\u7406\u89e3\u9519                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5c0f\u5fc3 Java .math.Mat&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/135877"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=135877"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/135877\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=135877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=135877"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=135877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}