zoukankan      html  css  js  c++  java
  • 大数字运算( BigInteger 、 BigDecimal)

    package com.lei.duixiang;
    
    import java.math.BigDecimal;
    import java.math.BigInteger;
    
    public class BigNum {
    
        /**
         * 大数字运算( BigInteger 、 BigDecimal)
         * 区别:BigDecimal 加入了小数的概念,它支持任何精度的定点数
         * @param args
         */
    
        // 大数字运算( BigInteger )
        public static void BigIntegerDemo(){
            BigInteger bigInteger = new BigInteger("4"); //实例化 一个大数字
            //取该大数字加 2 的运算
            System.out.println("加法操作:"+bigInteger.add(new BigInteger("2")));
            //取该大数字减 2 的运算
            System.out.println("减法操作:"+bigInteger.subtract(new BigInteger("2")));
            //取该大数字乘 2 的运算
            System.out.println("乘法操作:"+bigInteger.multiply(new BigInteger("2")));
            //取该大数字除 2 的运算
            System.out.println("除法操作:"+bigInteger.divide(new BigInteger("2")));
    
            //取该大数字除 3 的商
            System.out.println("取商:"+bigInteger.divideAndRemainder(new BigInteger("3"))[0]);
            //取该大数字除 3 的余数
            System.out.println("取商:"+bigInteger.divideAndRemainder(new BigInteger("3"))[1]);
            //取该大数字的 2 次方
            System.out.println("做2 次方操作:"+bigInteger.pow(2));
            //取该大数字的相反数
            System.out.println("取相反数操作:"+bigInteger.negate());
        }
    
        // 大数字运算( BigDecimal )
        static final int location = 10;
        /**
         * 定义加法方法,参数为加数与被加数
         * @param args
         */
        public BigDecimal add(double d1,double d2){        // 加法
            //实例化Decimal对象
            BigDecimal b1 = new BigDecimal(Double.toString(d1));
            BigDecimal b2 = new BigDecimal(Double.toString(d2));
            
            return b1.add(b2);    //调用 加法 方法
        }
        
        public BigDecimal sub(double d1,double d2){        // 减法
            //实例化Decimal对象
            BigDecimal b1 = new BigDecimal(Double.toString(d1));
            BigDecimal b2 = new BigDecimal(Double.toString(d2));
            
            return b1.subtract(b2);    //调用 减法 方法
        }
        
        public BigDecimal mul(double d1,double d2){        // 乘法
            //实例化Decimal对象
            BigDecimal b1 = new BigDecimal(Double.toString(d1));
            BigDecimal b2 = new BigDecimal(Double.toString(d2));
            
            return b1.multiply(b2);    //调用 乘法 方法
        }
        
        public BigDecimal div(double d1,double d2){        // 除法
            
            return div(d1,d2,location);    //调用 自定义   除法 方法
        }
        //定义出发方法,参数分别为除数与被除数以及商小数点后的位数
        public BigDecimal div(double d1,double d2,int b){
            if(b < 0){
                System.out.println(" b 值必须大于等于 0");
            }
            BigDecimal b1 = new BigDecimal(Double.toString(d1));
            BigDecimal b2 = new BigDecimal(Double.toString(d2));
            //调用除法方法,商小数点后保留 b位,并将结果进行四舍五入操作
            return b1.divide(b2, b, BigDecimal.ROUND_HALF_UP);
        }        // 除法
        
        
        
        public static void main(String[] args) {
            // 大数字运算( BigInteger )
            BigIntegerDemo();
            
            System.out.println("--------------------");
            BigNum b = new BigNum();
            System.out.println("两个数字相加结果:"+b.add(-7.5, 8.9));
            System.out.println("两个数字相减结果:"+b.sub(-7.5, 8.9));
            System.out.println("两个数字相乘结果:"+b.mul(-7.5, 8.9));
            System.out.println("两个数字相除结果,结果小数后保留 10 位:"+b.div(10, 2));
            System.out.println("两个数字相除,保留小数后5 位:"+b.div(-7.5, 8.9,5));
    
        }
    
    }
  • 相关阅读:
    自考新教材-p282
    p281
    自考新教材-p279_2
    用jmap分析java程序
    用jstack工具分析java程序
    java应用maven插件动态生成webservice代码
    Java对信号的处理
    【OracleDB】 01 概述和基本操作
    【Oracle】Windows-19C 下载安装
    【Hibernate】06 查询API
  • 原文地址:https://www.cnblogs.com/spadd/p/4170036.html
Copyright © 2011-2022 走看看