【java对象】--- BigDecimal的使用

  • BigDecimal的构造

   可以看出来BigDecimal对象给出的构造还是丰富的。下面是个简单的例子

        BigDecimal str = new BigDecimal("1");
        System.out.println(str);
        BigDecimal char1 = new BigDecimal('a');
        System.out.println(char1);
        char[] in = {'1','2','a','d'};
        BigDecimal char2 = new BigDecimal(in,0,2);//这里他会将'1','2'拼接,如果设置为3会出错
        System.out.println(char2);
        BigDecimal douDecimal = new BigDecimal(2.3d);
        System.out.println(douDecimal);
        BigDecimal floDecimal = new BigDecimal(2.3f);
        System.out.println(floDecimal);

    

  • BigDecimal的计算

  • BigDecimal 包含了:

  • 加法 add()函数 减法subtract()函数 

  • 乘法multipy()函数 除法divide()函数 绝对值abs()函数

BigDecimal str = new BigDecimal("2");
BigDecimal num = new BigDecimal(-1);

//加法
BigDecimal add = str.add(num);
System.out.println(add);

//减法
BigDecimal subtract = str.subtract(num);
System.out.println(subtract);

//乘法
BigDecimal multiply = str.multiply(num);
System.out.println(multiply);

//除法
BigDecimal divide = str.divide(num);
BigDecimal divide1 = str.divide(num,2,BigDecimal.ROUND_DOWN);//除法设置精度,和保留小数位策略
System.out.println(divide);
System.out.println(divide1);

BigDecimal abs = str.abs();//取绝对值
System.out.println(abs);

 

  • 舍入策略

  • 保留小数位常用的方式
  •     /**
         * Rounding mode to round away from zero.  Always increments the
         * digit prior to a nonzero discarded fraction.  Note that this rounding
         * mode never decreases the magnitude of the calculated value.
         */
        public final static int ROUND_UP =           0;//舍入远离零的舍入模式
    
        /**
         * Rounding mode to round towards zero.  Never increments the digit
         * prior to a discarded fraction (i.e., truncates).  Note that this
         * rounding mode never increases the magnitude of the calculated value.
         */
        public final static int ROUND_DOWN =         1;//接近零的舍入模式
    
        /**
         * Rounding mode to round towards positive infinity.  If the
         * {@code BigDecimal} is positive, behaves as for
         * {@code ROUND_UP}; if negative, behaves as for
         * {@code ROUND_DOWN}.  Note that this rounding mode never
         * decreases the calculated value.
         */
        public final static int ROUND_CEILING =      2;//接近正无穷大的舍入模式
    
        /**
         * Rounding mode to round towards negative infinity.  If the
         * {@code BigDecimal} is positive, behave as for
         * {@code ROUND_DOWN}; if negative, behave as for
         * {@code ROUND_UP}.  Note that this rounding mode never
         * increases the calculated value.
         */
        public final static int ROUND_FLOOR =        3;//接近负无穷大的舍入模式
    
        /**
         * Rounding mode to round towards {@literal "nearest neighbor"}
         * unless both neighbors are equidistant, in which case round up.
         * Behaves as for {@code ROUND_UP} if the discarded fraction is
         * ≥ 0.5; otherwise, behaves as for {@code ROUND_DOWN}.  Note
         * that this is the rounding mode that most of us were taught in
         * grade school.
         */
        public final static int ROUND_HALF_UP =      4;//四舍五入
    
        /**
         * Rounding mode to round towards {@literal "nearest neighbor"}
         * unless both neighbors are equidistant, in which case round
         * down.  Behaves as for {@code ROUND_UP} if the discarded
         * fraction is {@literal >} 0.5; otherwise, behaves as for
         * {@code ROUND_DOWN}.
         */
        public final static int ROUND_HALF_DOWN =    5;//五舍六入
    
        /**
         * Rounding mode to round towards the {@literal "nearest neighbor"}
         * unless both neighbors are equidistant, in which case, round
         * towards the even neighbor.  Behaves as for
         * {@code ROUND_HALF_UP} if the digit to the left of the
         * discarded fraction is odd; behaves as for
         * {@code ROUND_HALF_DOWN} if it's even.  Note that this is the
         * rounding mode that minimizes cumulative error when applied
         * repeatedly over a sequence of calculations.
         */
        //四舍六入,五分两种情况,如果前一位为奇数,则入位,否则舍去。
        public final static int ROUND_HALF_EVEN =    6;
    
        /**
         * Rounding mode to assert that the requested operation has an exact
         * result, hence no rounding is necessary.  If this rounding mode is
         * specified on an operation that yields an inexact result, an
         * {@code ArithmeticException} is thrown.
         */
        //断言请求的操作具有精确的结果,因此不需要舍入。
        //如果对获得精确结果的操作指定此舍入模式,则抛出ArithmeticException
        public final static int ROUND_UNNECESSARY =  7;

     

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值