BigDecimal 两束运算 保留两位小数

package com.example.bwjf.demo.util;

import java.math.BigDecimal;

public class NuberUtil {

/**
 *@description: number 小数位数 ,str 需要转换的字符
 *@author: wangl
 *@time:  2019/4/12 14:52
 *@version 1.0
 */
public static BigDecimal str2big(int number , String str){
    BigDecimal decimal = new BigDecimal(str);
    System.out.println(decimal);
    //向上四舍五入 2 位小数
    BigDecimal setScale = decimal.setScale(number,BigDecimal.ROUND_HALF_UP);
    System.out.println(setScale);

    return setScale;
}


/**
 *@description: b1,b2相乘 , 并且四舍五入保留number为小数
 *@author: wangl
 *@time:  2019/4/12 15:01
 *@version 1.0
 */
public static BigDecimal getResultMultiply(int number , String b1 , String b2 ){

    BigDecimal loanAmount = new BigDecimal(b1);
    BigDecimal interestRate = new BigDecimal(b2);
    BigDecimal interest = loanAmount.multiply(interestRate); //相乘

    //向上四舍五入 number 位小数
    BigDecimal setScale = interest.setScale(number,BigDecimal.ROUND_HALF_UP);
    System.out.println(setScale);

    return setScale;
}

/**
 *@description: 两数相除
 *@author: wangl
 *@time:  2019/4/16 15:14
 *@version 1.0
 */
public static BigDecimal getResultDivide(int number , String b1 , String b2 ){

    BigDecimal loanAmount = new BigDecimal(b1);
    BigDecimal interestRate = new BigDecimal(b2);
    BigDecimal interest = loanAmount.divide(interestRate); //触

    //向上四舍五入 number 位小数
    BigDecimal setScale = interest.setScale(number,BigDecimal.ROUND_HALF_UP);
    System.out.println(setScale);

    return setScale;
}


/**
 *@description: 两数相减
 *@author: wangl
 *@time:  2019/4/16 15:14
 *@version 1.0
 */
public static BigDecimal getResultSubtract(int number , String b1 , String b2 ){

    BigDecimal loanAmount = new BigDecimal(b1);
    BigDecimal interestRate = new BigDecimal(b2);
    BigDecimal interest = loanAmount.subtract(interestRate); //触

    //向上四舍五入 number 位小数
    BigDecimal setScale = interest.setScale(number,BigDecimal.ROUND_HALF_UP);
    System.out.println(setScale);

    return setScale;
}

}

你可能感兴趣的:(Java)