日常对于金额计算,应该都是用的 bigdecimal ,可是苦于没有好的工具类方法,现在贡献一个我正在用的对于数字计算的工具类,项目中就是用的这个,简单粗暴好用,话不多说,代码奉上(该工具类需要引入google的一个jar, com.google.common.base.optional ,具体 maven 引入看文章末尾):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
import java.math.bigdecimal; public class numberarithmeticutils { /** * bigdecimal的加法运算封装 * @param b1 * @param bn * @return */ public static bigdecimal safeadd(bigdecimal b1, bigdecimal... bn) { if ( null == b1) { b1 = bigdecimal.zero; } if ( null != bn) { for (bigdecimal b : bn) { b1 = b1.add( null == b ? bigdecimal.zero : b); } } return b1; } /** * integer加法运算的封装 * @param b1 第一个数 * @param bn 需要加的加法数组 * @注 : optional 是属于com.google.common.base.optional<t> 下面的class * @return */ public static integer safeadd(integer b1, integer... bn) { if ( null == b1) { b1 = 0 ; } integer r = b1; if ( null != bn) { for (integer b : bn) { r += optional.fromnullable(b).or( 0 ); } } return r > 0 ? r : 0 ; } /** * 计算金额方法 * @param b1 * @param bn * @return */ public static bigdecimal safesubtract(bigdecimal b1, bigdecimal... bn) { return safesubtract( true , b1, bn); } /** * bigdecimal的安全减法运算 * @param iszero 减法结果为负数时是否返回0,true是返回0(金额计算时使用),false是返回负数结果 * @param b1 被减数 * @param bn 需要减的减数数组 * @return */ public static bigdecimal safesubtract( boolean iszero, bigdecimal b1, bigdecimal... bn) { if ( null == b1) { b1 = bigdecimal.zero; } bigdecimal r = b1; if ( null != bn) { for (bigdecimal b : bn) { r = r.subtract(( null == b ? bigdecimal.zero : b)); } } return iszero ? (r.compareto(bigdecimal.zero) == - 1 ? bigdecimal.zero : r) : r; } /** * 整型的减法运算,小于0时返回0 * @param b1 * @param bn * @return */ public static integer safesubtract(integer b1, integer... bn) { if ( null == b1) { b1 = 0 ; } integer r = b1; if ( null != bn) { for (integer b : bn) { r -= optional.fromnullable(b).or( 0 ); } } return null != r && r > 0 ? r : 0 ; } /** * 金额除法计算,返回2位小数(具体的返回多少位大家自己看着改吧) * @param b1 * @param b2 * @return */ public static <t extends number> bigdecimal safedivide(t b1, t b2){ return safedivide(b1, b2, bigdecimal.zero); } /** * bigdecimal的除法运算封装,如果除数或者被除数为0,返回默认值 * 默认返回小数位后2位,用于金额计算 * @param b1 * @param b2 * @param defaultvalue * @return */ public static <t extends number> bigdecimal safedivide(t b1, t b2, bigdecimal defaultvalue) { if ( null == b1 || null == b2) { return defaultvalue; } try { return bigdecimal.valueof(b1.doublevalue()).divide(bigdecimal.valueof(b2.doublevalue()), 2 , bigdecimal.round_half_up); } catch (exception e) { return defaultvalue; } } /** * bigdecimal的乘法运算封装 * @param b1 * @param b2 * @return */ public static <t extends number> bigdecimal safemultiply(t b1, t b2) { if ( null == b1 || null == b2) { return bigdecimal.zero; } return bigdecimal.valueof(b1.doublevalue()).multiply(bigdecimal.valueof(b2.doublevalue())).setscale( 2 , bigdecimal.round_half_up); } } |
optional所在的jar以及版本: guava-18.0.ar
pom.xml配置:
1 2 3 4 5 6 |
<!-- https: //mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupid>com.google.guava</groupid> <artifactid>guava</artifactid> <version> 18.0 </version> </dependency> |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/moneyshi/article/details/65445820
查看更多关于Java使用BigDecimal进行运算封装的实际案例的详细内容...