Java常用类库----大数操作(BigInteger && BigDecimal)

可以使用BigIntger操作大整数

可以使用BigDecimal指定小数的保留位数

BigIntger

  如果在操作的时候一个整数数据已经超过了整数的最大类型长度long的话,则此数据就无法装入,所以,此时要使用BigInteger类进行操作。

    public BigInteger(String val)    将一个字符串变为BigInteger类型的数据库

    public BigInteger add(BigInteger val)    加法

    public BigInteger subtract(BigInteger val)  减法

    public BigInteger multiply(BigInteger val)    乘法

    public BigInteger divide(BigInteger val)    除法

    public BigInteger max(BigInteger val)    返回两个大数字中的最大值

    public BigInteger min(BigInteger val)    返回两个大数字中的最小值

    public BigInteger[] divideAndRemainder(BigInteger val)  除法操作,数组的第一个元素为除法的商,第二个元素为除法的余数

  注:发现divide()方法本身只是把最终的商保存下来,但是如果两个数无法整除呢,肯定存在余数

  BigInteger是在java.math包中

import java.math.BigInteger ;
public class BigIntegerDemo01{
    public static void main(String args[]){
        BigInteger bi1 = new BigInteger("123456789") ;    // 声明BigInteger对象
        BigInteger bi2 = new BigInteger("987654321") ;    // 声明BigInteger对象
        System.out.println("加法操作:" + bi2.add(bi1)) ;    // 加法操作
        System.out.println("减法操作:" + bi2.subtract(bi1)) ;    // 减法操作
        System.out.println("乘法操作:" + bi2.multiply(bi1)) ;    // 乘法操作
        System.out.println("除法操作:" + bi2.divide(bi1)) ;    // 除法操作
        System.out.println("最大数:" + bi2.max(bi1)) ;     // 求出最大数
        System.out.println("最小数:" + bi2.min(bi1)) ;     // 求出最小数
        BigInteger result[] = bi2.divideAndRemainder(bi1) ;    // 求出余数的除法操作
        System.out.println("商是:" + result[0] + 
            ";余数是:" + result[1]) ;
    }
};
View Code
除法操作:8
最大数:987654321
最小数:123456789
商是:8;余数是:9

BigDecimal

  使用此类可以完成大的小数操作,而且也可以使用此类进行精准的四舍五入,这一点在开发中经常使用。

  对于不需要任何准确计算精度的程序可以直接使用float或double完成,但是如果需要精准计算的结果,则必须使用BigDecimal类

    public BigDecimal(double val)    将double表示形式转换为BigDecimal

    public BigDecimal(int val)    将int表示形式转换为BigDecimal

    public BigDecimal(String val)    将字符串表示形式转换为BigDecimal

    public BigDecimal add(BigDecimal augend)    加法

    public BigDecimal subtract(BigDecimal subtractend)    减法

    public BigDecimal multiply(BigDecimal multiplicand)    乘法

    public BigDecimal divide(BigDecimal divide)    除法

    

import java.math.* ;
class MyMath{
    public static double add(double d1,double d2){        // 进行加法计算
        BigDecimal b1 = new BigDecimal(d1) ;
        BigDecimal b2 = new BigDecimal(d2) ;
        return b1.add(b2).doubleValue() ;
    }
    public static double sub(double d1,double d2){        // 进行减法计算
        BigDecimal b1 = new BigDecimal(d1) ;
        BigDecimal b2 = new BigDecimal(d2) ;
        return b1.subtract(b2).doubleValue() ;
    }
    public static double mul(double d1,double d2){        // 进行乘法计算
        BigDecimal b1 = new BigDecimal(d1) ;
        BigDecimal b2 = new BigDecimal(d2) ;
        return b1.multiply(b2).doubleValue() ;
    }
    public static double div(double d1,double d2,int len){        // 进行乘法计算
        BigDecimal b1 = new BigDecimal(d1) ;
        BigDecimal b2 = new BigDecimal(d2) ;
        return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue() ;
    }
    public static double round(double d,int len){    // 进行四舍五入
        BigDecimal b1 = new BigDecimal(d) ;
        BigDecimal b2 = new BigDecimal(1) ;
        return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue() ;
    }
};

public class Test{
    public static void main(String args[]){
        System.out.println("加法运算:" + MyMath.round(MyMath.add(10.345,3.333),1)) ;
        System.out.println("减法运算:" + MyMath.round(MyMath.sub(10.345,3.333),3)) ;
        System.out.println("乘法运算:" + MyMath.round(MyMath.mul(10.345,3.333),2)) ;
        System.out.println("除法运算:" + MyMath.div(10.345,3.333,3)) ;
    }
};
View Code
加法运算:13.7
减法运算:7.012
乘法运算:34.48
除法运算:3.104

  

 

posted @ 2015-04-07 17:27  闲来垂钓  阅读(982)  评论(0)    收藏  举报