BigDecimal usage

BigDecimal usage

import org.junit.Test;

import java.math.BigDecimal;
import java.text.NumberFormat;

import static java.math.BigDecimal.*;

/**
 * 对BigDecimal的所有的构造方法和使用方法进行测试
 * 常见的构造方法
 * BigDecimal(int)       创建一个具有参数所指定整数值的对象。
 * BigDecimal(double) 创建一个具有参数所指定双精度值的对象。
 * BigDecimal(long)    创建一个具有参数所指定长整数值的对象。
 * BigDecimal(String) 创建一个具有参数所指定以字符串表示的数值的对象。
 * <p>
 * 公共方法
 * add(BigDecimal)          BigDecimal对象中的值相加,然后返回这个对象。
 * subtract(BigDecimal)     BigDecimal对象中的值相减,然后返回这个对象。
 * multiply(BigDecimal)    BigDecimal对象中的值相乘,然后返回这个对象。
 * divide(BigDecimal)      BigDecimal对象中的值相除,然后返回这个对象。
 * toString()                将BigDecimal对象的数值转换成字符串。
 * doubleValue()          将BigDecimal对象中的值以双精度数返回。
 * floatValue()             将BigDecimal对象中的值以单精度数返回。
 * longValue()             将BigDecimal对象中的值以长整数返回。
 * intValue()               将BigDecimal对象中的值以整数返回
 * <p>
 * 计算方法
 * public BigDecimal add(BigDecimal value); //加法
 * public BigDecimal subtract(BigDecimal value); //减法
 * public BigDecimal multiply(BigDecimal value); //乘法
 * public BigDecimal divide(BigDecimal value); //除法
 * <p>
 * <p>
 * 格式化:
 * 由于NumberFormat类的format()方法可以使用BigDecimal对象作为其参数,
 * 可以利用BigDecimal对超出16位有效数字的货币值,百分值,以及一般数值进行格式化控制
 */
public class BigDecimalTest extends JavaDemoApplicationTests {



    @Test
    public void operationTest() {
        BigDecimal a = new BigDecimal("4.5");
        BigDecimal b = new BigDecimal("1.5");

        System.out.println("a + b =" + a.add(b));  //加法
        System.out.println("a - b =" + a.subtract(b));  //减法
        System.out.println("a * b =" + a.multiply(b));  //乘法
        System.out.println("a / b =" + a.divide(b));  //除法

    }


    @Test
    public void testConstruction() {
        //测试构造方法
        //参数为int
        BigDecimal bigDecimal = new BigDecimal(2);
        double k = 2.3;
        BigDecimal bDouble = new BigDecimal(k);
        //使用double作为参数,得到的结果会有损失,可以写成这样Double.toString(k)
        BigDecimal bdouble = new BigDecimal(Double.toString(k));
        //参数为String
        BigDecimal bString = new BigDecimal("2.3");

        System.out.println("bigDecimal=" + bigDecimal);
        System.out.println("bDouble=" + bDouble);
        System.out.println("bdouble=" + bdouble);
        System.out.println("bString=" + bString);

    }


    @Test
    public void divideTest() {
        BigDecimal a = new BigDecimal("8");
        BigDecimal b = new BigDecimal("3");
        // 除法的宁外一个方法,里面是3个参数,第一个是除数类型是bigdecimal,第二个参数是保留小数点的位数,第三个参数是你选择的进舍的规则
        //保留两位小数,如果两位小数后面还有值向前进位
        System.out.println(a.divide(b, 0, ROUND_CEILING));
        System.out.println(a.divide(b, 2, ROUND_UP));
    }

    @Test
    public void formatTest() {
        NumberFormat currency = NumberFormat.getCurrencyInstance(); //建立货币格式化引用
        NumberFormat percent = NumberFormat.getPercentInstance();  //建立百分比格式化引用
        percent.setMaximumFractionDigits(3); //百分比小数点最多3位

        BigDecimal loanAmount = new BigDecimal("15000.48"); //贷款金额
        BigDecimal interestRate = new BigDecimal("0.008"); //利率
        BigDecimal interest = loanAmount.multiply(interestRate); //相乘

        System.out.println("贷款金额:\t" + currency.format(loanAmount));
        System.out.println("利率:\t" + percent.format(interestRate));
        System.out.println("利息:\t" + currency.format(interest));

    }


    @Test
    public void divideTests() {
        //绝对值的使用
        System.out.println(new BigDecimal(-1));
        System.out.println(new BigDecimal(-1).abs());
    }


}

BigDecimal provides 8 rounding
1, ROUND_UP: rounding away from zero rounding mode. Before dropping parts are increasing numbers of non-zero (non-zero number of discarded portion is always in front of the plus 1).
Note that this rounding mode never decreases the size of the calculated value.

2, ROUND_DOWN: close to zero rounding mode. Before dropping a part of the digital still do not increase (never dropped off the front of the number plus 1, i.e. truncation).
Note that this rounding mode never increases the size of the calculated value.

3, ROUND_CEILING: approaching positive infinity rounding mode. If the BigDecimal is positive, the same rounding behavior ROUND_UP; if it is negative, then rounding behavior ROUND_DOWN same.
Note that this rounding mode never decreases the calculated value.

4, ROUND_FLOOR: approaching negative infinity rounding mode. If the BigDecimal is positive, the same rounding behavior ROUND_DOWN; if it is negative, then rounding behavior ROUND_UP same.
Note that this rounding mode never increases the calculated value.

5, ROUND_HALF_UP: the "nearest neighbor" rounding, if the distance between two adjacent equal numbers, was rounded up rounding mode.
If the discarded fraction is> = 0.5, the same rounding behavior ROUND_UP; otherwise the same rounding behavior ROUND_DOWN.
Note that this is most of us were taught in elementary school when the rounding mode (rounded).

6, ROUND_HALF_DOWN: the "nearest neighbor" rounding, if equal to the distance between two adjacent digits, compared with the rounding rounding mode.
If the discarded fraction is> 0.5, the rounding behavior is the same ROUND_UP; otherwise rounding behavior is the same ROUND_DOWN (five off and six).

7, ROUND_HALF_EVEN: the "nearest neighbor" rounding, if the numbers are equal distance from two adjacent, the adjacent even to rounding.
If discarded digit to the left is an odd number, the rounding behavior is the same as ROUND_HALF_UP;
if it is even, then the rounding behavior ROUND_HALF_DOWN same.
Note that, in repeating a series of calculations, this rounding mode may be accumulated error is minimized.
This rounding mode is also known as the "banker's rounding method", mainly in the United States. Four off and six, fifth in both cases. If the former is odd, in place, otherwise discarded. The following is an example of a decimal places, then this results in the rounding mode.
1.15> 1.2 1.25> 1.2

8, ROUND_UNNECESSARY: assert that the requested operation has an exact result, hence no rounding. If this rounding mode is specified for operation obtain accurate results is thrown ArithmeticException.

Guess you like

Origin www.cnblogs.com/java-hardly-road/p/11102543.html