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

时间:2023-03-09 08:48:39
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:银行家舍入法。

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

 /**
* Aaron.ffp Inc.
* Copyright (c) 2004-2016 All Rights Reserved.
*/
package cn.ffp.autotest.api.util; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat; import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator; import cn.ffp.autotest.api.settings.CONSINFO; /**
* <strong>计算工具类</strong><br>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-api cn.ffp.autotest.api.util MathUtil.java, 2016-04-12 17:51:58.301 Exp $
*/
public class MathUtil {
private static Logger logger = Logger.getLogger(MathUtil.class.getName());
private static String msg = ""; public MathUtil() {
DOMConfigurator.configure(CONSINFO.CONF_LOG4J_XML);
} /**
* <strong>对数值进行格式化输出</strong><br>
* <ul>
* <li>例如:format("2.23956", 3)的结果为:2.240</li>
* </ul>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-api cn.ffp.autotest.api.util MathUtil.java format, 2016-04-12 20:13:43.664 Exp $
*
* @param digit 待格式化数值
* @param scale 保留位数(小于1时为整数)
* @return 格式化数字字符串
*/
public static String format(double digit, int scale) {
String format = "#."; if (scale < 1) {
format = "#";
} for (int i = 0; i < scale; i++) {
format += "0";
} return MathUtil.format(digit, format);
} /**
* <strong>对数值进行格式化输出</strong><br>
* <ul>
* <li>格式化样式示例(#.00,表示保留2位;#.0000,表示保留4位)</li>
* </ul>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-api cn.ffp.autotest.api.util MathUtil.java format, 2016-04-12 19:44:00.926 Exp $
*
* @param digit 待格式化数值
* @param format 格式化样式
* @return 格式化数值字符串
*/
private static String format(double digit, String format) {
try {
DecimalFormat decimalFormat = new DecimalFormat(format); return decimalFormat.format(digit);
} catch (NullPointerException npe) {
msg = "将数字【" + digit + "】依据样式【" + format + "】格式化失败,原因:";
logger.error(msg, npe); return null;
} catch (IllegalArgumentException iae) {
msg = "将数字【" + digit + "】依据样式【" + format + "】格式化失败,原因:";
logger.error(msg, iae); return null;
}
} /**
* <strong>对数值进行四舍五入</strong><br>
* <ul>
* <li>采用银行家舍入法</li>
* </ul>
* <br>
* @author Aaron.ffp
* @version V1.0.0: autotest-api cn.ffp.autotest.api.util MathUtil.java scale, 2016-04-12 19:42:52.068 Exp $
*
* @param digit 数值
* @param scale 保留位数
* @return 四舍五入后的数值
*/
public static String scale(String digit, int scale) {
try {
if (scale < 0) {
msg = "对【" + digit + "】进行四舍五入失败,原因:指定位数【" + scale + "】不可小于0!请检查!";
logger.warn(msg); return null;
} return new BigDecimal(digit).setScale(scale, RoundingMode.HALF_EVEN).toString();
} catch (NumberFormatException nfe) {
msg = "获取【" + digit + "】指定位数【" + scale + "】四舍五入失败,原因:";
logger.error(msg, nfe);
} catch (ArithmeticException ae) {
msg = "获取【" + digit + "】指定位数【" + scale + "】四舍五入失败,原因:";
logger.error(msg, ae);
} return null;
}
}

  

  对应测试源码:

 package cn.ffp.autotest.api.util;

 import org.testng.annotations.Test;

 public class MathUtilTest {
@Test(description = "public static String format(double digit, int scale) --- 测试")
public void test_format() {
System.out.println("MathUtil.format(\"2.23956\", 3) \t " + MathUtil.format(2.23956, 3));
System.out.println("MathUtil.format(\"2.23956\", 0) \t " + MathUtil.format(2.23956, 0));
System.out.println("MathUtil.format(\"2.23956\", -34) \t " + MathUtil.format(2.23956, -34));
} @Test(description = "public static String scale(String digit, int scale) --- 测试")
public void test_scale() {
System.out.println("MathUtil.scale(\"2.23956\", 3) \t " + MathUtil.scale("2.23956", 3));
System.out.println("MathUtil.scale(\"2.23956\", 0) \t " + MathUtil.scale("2.23956", 0));
System.out.println("MathUtil.scale(\"2.23956\", -3) \t " + MathUtil.scale("2.23956", -3));
}
}

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

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