Java学习-047-数值格式化及小数位数四舍五入

此小工具类主要用于数值四舍五入、数值格式化输出,很简单,若想深入研究,敬请自行查阅 BigDecimal 或 DecimalFormat 的 API,BigDecimal.setScale(位数,四舍五入法)中四舍五入法有如下 7 种:

         1、 ROUND_UP:远离零方向舍入。向绝对值最大的方向舍入,只要舍弃位非0即进位。

         2、 ROUND_DOWN:趋向零方向舍入。向绝对值最小的方向输入,所有的位都要舍弃,不存在进位情况。

         3、 ROUND_CEILING:向正无穷方向舍入。向正最大方向靠拢。若是正数,舍入行为类似于ROUND_UP,若为负数,舍入行为类似于ROUND_DOWN。Math.round()方法就是使用的此模式。

         4、 ROUND_FLOOR:向负无穷方向舍入。向负无穷方向靠拢。若是正数,舍入行为类似于ROUND_DOWN;若为负数,舍入行为类似于ROUND_UP。

         5、 HALF_UP:最近数字舍入(5进)。这是我们最经典的四舍五入。

         6、 HALF_DOWN:最近数字舍入(5舍)。在这里5是要舍弃的。

         7、 HAIL_EVEN:银行家舍入法。

不多说,直接上码,如下所示:

  1 /**
  2  * Aaron.ffp Inc.
  3  * Copyright (c) 2004-2016 All Rights Reserved.
  4  */
  5 package cn.ffp.autotest.api.util;
  6 
  7 import java.math.BigDecimal;
  8 import java.math.RoundingMode;
  9 import java.text.DecimalFormat;
 10 
 11 import org.apache.log4j.Logger;
 12 import org.apache.log4j.xml.DOMConfigurator;
 13 
 14 import cn.ffp.autotest.api.settings.CONSINFO;
 15 
 16 /**
 17  * <strong>计算工具类</strong><br>
 18  * <br>
 19  * @author Aaron.ffp
 20  * @version V1.0.0: autotest-api cn.ffp.autotest.api.util MathUtil.java, 2016-04-12 17:51:58.301 Exp $
 21  */
 22 public class MathUtil {
 23     private static Logger logger = Logger.getLogger(MathUtil.class.getName());
 24     private static String msg    = "";
 25     
 26     public MathUtil() {
 27         DOMConfigurator.configure(CONSINFO.CONF_LOG4J_XML);
 28     }
 29     
 30     /**
 31      * <strong>对数值进行格式化输出</strong><br>
 32      * <ul>
 33      * <li>例如:format("2.23956", 3)的结果为:2.240</li>
 34      * </ul>
 35      * <br>
 36      * @author Aaron.ffp
 37      * @version V1.0.0: autotest-api cn.ffp.autotest.api.util MathUtil.java format, 2016-04-12 20:13:43.664 Exp $
 38      * 
 39      * @param digit 待格式化数值
 40      * @param scale 保留位数(小于1时为整数)
 41      * @return 格式化数字字符串
 42      */
 43     public static String format(double digit, int scale) {
 44         String format = "#.";
 45         
 46         if (scale < 1) {
 47             format = "#";
 48         }
 49         
 50         for (int i = 0; i < scale; i++) {
 51             format += "0";
 52         }
 53         
 54         return MathUtil.format(digit, format);
 55     }
 56     
 57     /**
 58      * <strong>对数值进行格式化输出</strong><br>
 59      * <ul>
 60      * <li>格式化样式示例(#.00,表示保留2位;#.0000,表示保留4位)</li>
 61      * </ul>
 62      * <br>
 63      * @author Aaron.ffp
 64      * @version V1.0.0: autotest-api cn.ffp.autotest.api.util MathUtil.java format, 2016-04-12 19:44:00.926 Exp $
 65      * 
 66      * @param digit 待格式化数值
 67      * @param format 格式化样式
 68      * @return 格式化数值字符串
 69      */
 70     private static String format(double digit, String format) {
 71         try {
 72             DecimalFormat decimalFormat = new DecimalFormat(format);
 73             
 74             return decimalFormat.format(digit);
 75         } catch (NullPointerException npe) {
 76             msg = "将数字【" + digit + "】依据样式【" + format + "】格式化失败,原因:";
 77             logger.error(msg, npe);
 78             
 79             return null;
 80         } catch (IllegalArgumentException iae) {
 81             msg = "将数字【" + digit + "】依据样式【" + format + "】格式化失败,原因:";
 82             logger.error(msg, iae);
 83             
 84             return null;
 85         }
 86     }
 87     
 88     /**
 89      * <strong>对数值进行四舍五入</strong><br>
 90      * <ul>
 91      * <li>采用银行家舍入法</li>
 92      * </ul>
 93      * <br>
 94      * @author Aaron.ffp
 95      * @version V1.0.0: autotest-api cn.ffp.autotest.api.util MathUtil.java scale, 2016-04-12 19:42:52.068 Exp $
 96      * 
 97      * @param digit 数值
 98      * @param scale 保留位数
 99      * @return 四舍五入后的数值
100      */
101     public static String scale(String digit, int scale) {
102         try {
103             if (scale < 0) {
104                 msg = "对【" + digit + "】进行四舍五入失败,原因:指定位数【" + scale + "】不可小于0!请检查!";
105                 logger.warn(msg);
106                 
107                 return null;
108             }
109             
110             return new BigDecimal(digit).setScale(scale, RoundingMode.HALF_EVEN).toString();
111         } catch (NumberFormatException nfe) {
112             msg = "获取【" + digit + "】指定位数【" + scale + "】四舍五入失败,原因:";
113             logger.error(msg, nfe);
114         } catch (ArithmeticException ae) {
115             msg = "获取【" + digit + "】指定位数【" + scale + "】四舍五入失败,原因:";
116             logger.error(msg, ae);
117         }
118         
119         return null;
120     }
121 }

  

  对应测试源码:

 1 package cn.ffp.autotest.api.util;
 2 
 3 import org.testng.annotations.Test;
 4 
 5 public class MathUtilTest {
 6     @Test(description = "public static String format(double digit, int scale) --- 测试")
 7     public void test_format() {
 8         System.out.println("MathUtil.format("2.23956", 3) 	 " + MathUtil.format(2.23956, 3));
 9         System.out.println("MathUtil.format("2.23956", 0) 	 " + MathUtil.format(2.23956, 0));
10         System.out.println("MathUtil.format("2.23956", -34) 	 " + MathUtil.format(2.23956, -34));
11     }
12 
13     @Test(description = "public static String scale(String digit, int scale) --- 测试")
14     public void test_scale() {
15         System.out.println("MathUtil.scale("2.23956", 3) 	 " + MathUtil.scale("2.23956", 3));
16         System.out.println("MathUtil.scale("2.23956", 0) 	 " + MathUtil.scale("2.23956", 0));
17         System.out.println("MathUtil.scale("2.23956", -3) 	 " + MathUtil.scale("2.23956", -3));
18     }
19 }

至此, Java学习-047-数值格式化及小数位数四舍五入顺利完结,希望此文能够给初学 Java 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

 

原文地址:https://www.cnblogs.com/fengpingfan/p/5384366.html