我是小懒猴,人狠话不多,直接上才艺。
BigDecimal 相关操作-> 待补充
BigDecimal add = ma. add ( mb) ;
System . out. println ( "加法:" + add) ;
BigDecimal sub = ma. subtract ( mb) ;
System . out. println ( "减法:" + sub) ;
BigDecimal mul = mb. multiply ( md) ;
System . out. println ( "乘法:" + mul) ;
BigDecimal div = mb. divide ( md) ;
System . out. println ( "除法:" + div) ;
mc = mc. setScale ( 2 , BigDecimal . ROUND_HALF_UP ) ;
System . out. println ( "四舍五入:" + mc) ;
mc = mc. negate ( ) ;
System . out. println ( "负数:" + mc) ;
divide-四舍五入
public BigDecimal divide ( BigDecimal divisor, int scale, int roundingMode)
第一个参数是除数,第二个参数代表保留几位小数,第三个代表的是使用的模式。、
BigDecimal . ROUND_DOWN : 直接省略多余的小数,比如1.28 如果保留1 位小数,得到的就是1.2
BigDecimal . ROUND_UP : 直接进位,比如1.21 如果保留1 位小数,得到的就是1.3
BigDecimal . ROUND_HALF_UP : 四舍五入,2.35 保留1 位,变成2.4
BigDecimal . ROUND_HALF_DOWN : 四舍五入,2.35 保留1 位,变成2.3
后边两种的区别就是如果保留的位数的后一位如果正好是5 的时候,一个舍弃掉,一个进位。
例子:
BigDecimal magInterest = fusion. getRzAmt ( )
. multiply ( fusion. getMagRate ( ) )
. multiply ( new BigDecimal ( days) )
. divide ( new BigDecimal ( "360" ) , 2 , BigDecimal . ROUND_HALF_UP ) ;
比较大小
BigDecimal b1 = new BigDecimal ( 0.1 ) ;
BigDecimal b2 = new BigDecimal ( 0.2 ) ;
int c = b1. compareTo ( b2) ;
c= 1 表示b1大于b2
c= 0 表示b1等于b2
c= - 1 表示b1小于b2
判断只有五种情况
> 0 a大于b
< 0 a小于b
= 0 相等
>= 0 a大于等于b
<= 0 a小于等于b
设置小数位数
BigDecimal bigDecimal1 = new BigDecimal ( 88.128 ) ;
bigDecimal1. setScale ( 2 , BigDecimal . ROUND_DOWN ) ;
BigDecimal . valueOf ( 1.234 ) . setScale ( 2 , BigDecimal . ROUND_HALF_DOWN ) = 1.23
BigDecimal . valueOf ( 1.235 ) . setScale ( 2 , BigDecimal . ROUND_HALF_DOWN ) = 1.23
BigDecimal . valueOf ( 1.236 ) . setScale ( 2 , BigDecimal . ROUND_HALF_DOWN ) = 1.24
BigDecimal . valueOf ( 1.234 ) . setScale ( 2 , BigDecimal . ROUND_HALF_UP ) = 1.23
BigDecimal . valueOf ( 1.235 ) . setScale ( 2 , BigDecimal . ROUND_HALF_UP ) = 1.24
BigDecimal . valueOf ( 1.236 ) . setScale ( 2 , BigDecimal . ROUND_HALF_UP ) = 1.24
shareBenRuleVo. setAddValTax ( shareBenRuleVo. getAddValTax ( ) . setScale ( 2 , BigDecimal . ROUND_DOWN ) ) ;
判断传来是否为空
如果该字段可以为空 , 可以用null 来判断。
buyerPayAddVo. getBuyerPay ( ) . stream ( ) . forEach ( buyerPayAddVo1 -> {
if ( buyerPayAddVo1. getPayInterestRatio ( ) != null ) {
buyerPayAddVo1. setPayInterestRatio ( buyerPayAddVo1. getPayInterestRatio ( ) . setScale ( 2 , BigDecimal . ROUND_DOWN ) ) ;
if ( buyerPayAddVo1. getPayInterestRatio ( ) . compareTo ( new BigDecimal ( 0 ) ) < 0 ) {
throw new BusinessException ( "买方付息比例payInterestRatio买方付息比例必须大于或等于0" ) ;
}
if ( buyerPayAddVo1. getPayInterestRatio ( ) . compareTo ( new BigDecimal ( 100 ) ) > 0 ) {
throw new BusinessException ( "买方付息比例payInterestRatio买方付息比例必须小于或等于100" ) ;
}
}
DecimalFormat
BigDecimal bigDecimal = new BigDecimal ( "123.11" ) ;
DecimalFormat decimalFormat = new DecimalFormat ( "#.00" ) ;
System . out. println ( decimalFormat. format ( bigDecimal) ) ;
BigDecimal bigDecimal1 = new BigDecimal ( "123" ) ;
DecimalFormat decimalFormat1 = new DecimalFormat ( "#.00" ) ;
System . out. println ( decimalFormat. format ( bigDecimal1) ) ;
0 没有则补0
DecimalFormat df = new DecimalFormat ( "0.00" ) ;
如果小数点前为0 则为0 ;结果为0.00
总和
String loanAmt = strings. stream ( ) . map ( y -> y. getAmount ( ) )
. reduce ( BigDecimal . ZERO , BigDecimal :: add ) + "" ;
求和
BigDecimal sign = res. stream ( ) . filter ( ( b) -> Arrays . asList ( "02" , "04" )
. contains ( b. getSxStatus ( ) ) ) . map ( TSxInfo :: getReceiveAmt )
. reduce ( BigDecimal :: add ) . orElse ( BigDecimal . ZERO ) ;
判断字段为null ,并且设置默认值为0
ArrayList < tec> tecs = new ArrayList < > ( ) ;
tec tec = new tec ( ) ;
tec. setAmt ( null ) ;
tec tec1 = new tec ( ) ;
tec1. setAmt ( null ) ;
tecs. add ( tec) ;
tecs. add ( tec1) ;
System . out. println ( tecs. stream ( ) . filter ( x -> x. getAmt ( ) != null ) . map ( class01. tec:: getAmt ) . reduce ( BigDecimal :: add ) . orElse ( new BigDecimal ( 0 ) ) ) ;