{"id":145,"date":"2020-07-04T14:06:56","date_gmt":"2020-07-04T06:06:56","guid":{"rendered":"https:\/\/22pig.com\/?p=145"},"modified":"2020-07-04T14:06:56","modified_gmt":"2020-07-04T06:06:56","slug":"java-doubleutil","status":"publish","type":"post","link":"http:\/\/22pig.com\/?p=145","title":{"rendered":"[java] DoubleUtil \u6570\u503c\u8f6c\u6362\u5de5\u5177\u7c7b"},"content":{"rendered":"<p>package com.22pig.util;<\/p>\n<p>import java.math.RoundingMode;<br \/>\nimport java.text.DecimalFormat;<br \/>\nimport java.text.NumberFormat;<\/p>\n<p>import org.apache.commons.lang.StringUtils;<\/p>\n<p>public class DoubleUtil {<\/p>\n<p>\/**<br \/>\n* NumberFormat is the abstract base class for all number formats.<br \/>\n* This class provides the interface for formatting and parsing numbers.<br \/>\n* @param d<br \/>\n* @return<br \/>\n*\/<br \/>\npublic static String formatDoubleUp(double d,int len) {<br \/>\nreturn String.format(&#8220;%.&#8221;+len+&#8221;f&#8221;, d);<br \/>\n}<\/p>\n<p>public static String formatDoubleUp(String d,int len) {<br \/>\nif(d.contains(&#8220;.&#8221;)){<br \/>\nlen = d.indexOf(&#8220;.&#8221;)+len;<br \/>\nif(len&gt;d.length()) len = d.length();<br \/>\nd= d.substring(0,len);<br \/>\n}<br \/>\nreturn d;<br \/>\n}<\/p>\n<p>\/**<br \/>\n* NumberFormat is the abstract base class for all number formats.<br \/>\n* This class provides the interface for formatting and parsing numbers.<br \/>\n* @param d<br \/>\n* @return<br \/>\n*\/<br \/>\npublic static String formatDoubleDown(double d,int len) {<br \/>\nNumberFormat nf = new DecimalFormat(&#8220;#.#&#8221;);<br \/>\nnf.setMaximumFractionDigits(len); \/\/ \u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570<br \/>\nnf.setRoundingMode(RoundingMode.DOWN);\/\/ \u4e0d\u9700\u8981\u56db\u820d\u4e94\u5165<br \/>\nreturn nf.format(d);<br \/>\n}<\/p>\n<p>public static String addDouble(String[] doubleStrArr){<br \/>\ndouble sum =0;<br \/>\nfor(int i=0;i&lt;doubleStrArr.length;i++){<br \/>\ndouble tmp = parseDouble(doubleStrArr[i]);<br \/>\nsum+= tmp;<br \/>\n}<br \/>\nreturn String.valueOf(sum);<br \/>\n}<\/p>\n<p>public static double parseDouble(String str){<br \/>\nif(StringUtils.isBlank(str) || &#8220;null&#8221;.equals(str.trim())){<br \/>\nstr = &#8220;0&#8221;;<br \/>\n}<br \/>\nreturn Double.parseDouble(str);<br \/>\n}<\/p>\n<p>\/\/s\u4e3anull\u6216&#8221;&#8221;\u65f6\u5019\u8fd4\u56de0<br \/>\npublic static String null2o(String s) {<br \/>\nreturn (s == null||s.equals(&#8220;&#8221;)) ? &#8220;0&#8221; : s;<br \/>\n}<\/p>\n<p>public static double[] parseDouble(String[] dss){<br \/>\ndouble[] ds = new double[dss.length];<br \/>\nfor(int i=0;i&lt;dss.length;i++){<br \/>\nds[i] = parseDouble(dss[i]);<br \/>\n}<br \/>\nreturn ds;<br \/>\n}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>package com.22pig.util; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.NumberFormat; import org.apache.commons.lang.StringUtils; public class DoubleUtil { \/** * NumberFormat is the abstract base class for all number formats. * This class provides the interface for formatting and parsing numbers. * @param d * @return *\/ public static String formatDoubleUp(double d,int len) { return String.format(&#8220;%.&#8221;+len+&#8221;f&#8221;, d); } public [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-145","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=145"}],"version-history":[{"count":0,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/145\/revisions"}],"wp:attachment":[{"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}