1 packagecn.com.cxsw.utils;2
3 importjava.math.BigDecimal;4
5 /**
6 * 与小数位精度(四舍五入等)相关的一些常用工具方法.7 *8 * float/double的精度取值方式分为以下几种:
9 * java.math.BigDecimal.ROUND_UP
10 * java.math.BigDecimal.ROUND_DOWN
11 * java.math.BigDecimal.ROUND_CEILING
12 * java.math.BigDecimal.ROUND_FLOOR
13 * java.math.BigDecimal.ROUND_HALF_UP
14 * java.math.BigDecimal.ROUND_HALF_DOWN
15 * java.math.BigDecimal.ROUND_HALF_EVEN
16 *17 * @title RoundTool18 * @describe19 *@authorzfc20 * @date 2017年10月25日上午11:18:4721 */
22
23 public final classRoundTool {24 /**
25 * 对double数据进行取精度.26 *
27 * For example:
28 * double value = 100.345678;
29 * double ret = round(value,4,BigDecimal.ROUND_HALF_UP);
30 * ret为100.3457
31 *32 *@paramvalue33 * double数据.34 *@paramscale35 * 精度位数(保留的小数位数).36 *@paramroundingMode37 * 精度取值方式.38 *@return精度计算后的数据.39 */
40 public static double round(double value, int scale, introundingMode) {41 BigDecimal bd = newBigDecimal(value);42 bd =bd.setScale(scale, roundingMode);43 double d =bd.doubleValue();44 bd = null;45 returnd;46 }47
48 /**
49 * 测试用的main方法.50 *51 *@paramargc52 * 运行参数.53 *54 */
55 public static voidmain(String[] argc) {56 //下面都以保留2位小数为例57
58 //ROUND_UP59 //只要第2位后面存在大于0的小数,则第2位就+1
60 System.out.println(round(12.3401, 2, BigDecimal.ROUND_UP));//12.35
61 System.out.println(round(-12.3401, 2, BigDecimal.ROUND_UP));//-12.3562
63 //ROUND_DOWN64 //与ROUND_UP相反65 //直接舍弃第2位后面的所有小数
66 System.out.println(round(12.349, 2, BigDecimal.ROUND_DOWN));//12.34
67 System.out.println(round(-12.349, 2, BigDecimal.ROUND_DOWN));//-12.3468
69 //ROUND_CEILING70 //如果数字>0 则和ROUND_UP作用一样71 //如果数字<0 则和ROUND_DOWN作用一样
72 System.out.println(round(12.3401, 2, BigDecimal.ROUND_CEILING));//12.35
73 System.out.println(round(-12.349, 2, BigDecimal.ROUND_CEILING));//-12.3474
75 //ROUND_FLOOR76 //如果数字>0 则和ROUND_DOWN作用一样77 //如果数字<0 则和ROUND_UP作用一样
78 System.out.println(round(12.349, 2, BigDecimal.ROUND_FLOOR));//12.34
79 System.out.println(round(-12.3401, 2, BigDecimal.ROUND_FLOOR));//-12.3580
81 //ROUND_HALF_UP [这种方法最常用,四舍五入]82 //如果第3位数字>=5,则第2位数字+183 //备注:只看第3位数字的值,不会考虑第3位之后的小数的
84 System.out.println(round(12.345, 2, BigDecimal.ROUND_HALF_UP));//12.35
85 System.out.println(round(12.3449, 2, BigDecimal.ROUND_HALF_UP));//12.34
86 System.out.println(round(-12.345, 2, BigDecimal.ROUND_HALF_UP));//-12.35
87 System.out.println(round(-12.3449, 2, BigDecimal.ROUND_HALF_UP));//-12.3488
89 //ROUND_HALF_DOWN90 //如果第3位数字>=5,则做ROUND_UP91 //如果第3位数字<5,则做ROUND_DOWN
92 System.out.println(round(12.345, 2, BigDecimal.ROUND_HALF_DOWN));//12.35
93 System.out.println(round(12.3449, 2, BigDecimal.ROUND_HALF_DOWN));//12.34
94 System.out.println(round(-12.345, 2, BigDecimal.ROUND_HALF_DOWN));//-12.35
95 System.out.println(round(-12.3449, 2, BigDecimal.ROUND_HALF_DOWN));//-12.3496
97 //ROUND_HALF_EVEN98 //如果第3位是偶数,则做ROUND_HALF_DOWN99 //如果第3位是奇数,则做ROUND_HALF_UP
100 System.out.println(round(12.346, 2, BigDecimal.ROUND_HALF_EVEN));//12.35
101 System.out.println(round(12.345, 2, BigDecimal.ROUND_HALF_EVEN));//12.35
102
103 }104
105 }