JAVA BigDecimal and BigInteger

import java.math.BigDecimal;
import java.util.*;
/**    
* @ClassName: ArithUtils   
* @Description: 数学计算工具类   
* 由于Java的简单类型不能够精确的对浮点数进行运算,这个工具类提供精 确的浮点数运算,包括加减乘除和四舍五入。  
* @author shaojian.yu  
* @date 2015年11月9日 下午5:44:12   
*/    
public class Main {
    // 默认除法运算精度    
    private static final int DEF_DIV_SCALE = 50;    

    // 这个类不能实例化    
    private Main() {    
    }    

    /**  
     * @Title:add  
     * @Description: 提供精确的加法运算。   
     * @author shaojian.yu   
     * @date 2015年11月9日 下午5:46:55  
     * @param v1 被加数  
     * @param v2 加数  
     * @return 两个参数的和  
     */    
    public static double add(double v1, double v2) {    
        BigDecimal b1 = new BigDecimal(Double.toString(v1));    
        BigDecimal b2 = new BigDecimal(Double.toString(v2));    
        return b1.add(b2).doubleValue();    
    }    

    /**  
     * @Title:sub  
     * @Description: 提供精确的减法运算。   
     * @author shaojian.yu   
     * @date 2015年11月9日 下午5:47:24  
     * @param v1 被减数  
     * @param v2 减数  
     * @return 两个参数的差  
     */    
    public static double sub(double v1, double v2) {    
        BigDecimal b1 = new BigDecimal(Double.toString(v1));    
        BigDecimal b2 = new BigDecimal(Double.toString(v2));    
        return b1.subtract(b2).doubleValue();    
    }    

    /**  
     *   
     * @Title:mul  
     * @Description: 提供精确的乘法运算。   
     * @author shaojian.yu   
     * @date 2015年11月9日 下午5:47:51  
     * @param v1 被乘数  
     * @param v2 乘数  
     * @return 两个参数的积  
     */    
    public static double mul(double v1, double v2) {    
        BigDecimal b1 = new BigDecimal(Double.toString(v1));    
        BigDecimal b2 = new BigDecimal(Double.toString(v2));    
        return b1.multiply(b2).doubleValue();    
    }    

    /**  
     *   
     * @Title:div  
     * @Description: 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到小数点以后10位,以后的数字四舍五入。   
     * @author shaojian.yu   
     * @date 2015年11月9日 下午5:48:10  
     * @param v1 被除数  
     * @param v2 除数  
     * @return 两个参数的商  
     */    
    public static double div(double v1, double v2) {    
        return div(v1, v2, DEF_DIV_SCALE);    
    }    

    /**  
     *   
     * @Title:div  
     * @Description: 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指定精度,以后的数字四舍五入。   
     * @author shaojian.yu   
     * @date 2015年11月9日 下午5:48:50  
     * @param v1 被除数  
     * @param v2 除数  
     * @param scale 表示需要精确到小数点以后几位。  
     * @return 两个参数的商  
     */    
    public static double div(double v1, double v2, int scale) {    
        if (scale < 0) {    
            throw new IllegalArgumentException(    
                    "The   scale   must   be   a   positive   integer   or   zero");    
        }    
        BigDecimal b1 = new BigDecimal(Double.toString(v1));    
        BigDecimal b2 = new BigDecimal(Double.toString(v2));    
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();    
    }    

    /**  
     *   
     * @Title:round  
     * @Description: 提供精确的小数位四舍五入处理。   
     * @author shaojian.yu   
     * @date 2015年11月9日 下午5:49:13  
     * @param v 需要四舍五入的数字  
     * @param scale 小数点后保留几位  
     * @return 四舍五入后的结果  
     */    
    public static double round(double v, int scale) {    
        if (scale < 0) {    
            throw new IllegalArgumentException(    
                    "The   scale   must   be   a   positive   integer   or   zero");    
        }    
        BigDecimal b = new BigDecimal(Double.toString(v));    
        BigDecimal one = new BigDecimal("1");    
        return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();    
    }   
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a, b;
        while (cin.hasNext()){
            a = cin.nextInt();
            b = cin.nextInt();
            double c = div(a, b, 50);
            System.out.println(a + b);
        }
    }

}
BigInteger abs()  返回大整数的绝对值
BigInteger add(BigInteger val) 返回两个大整数的和
BigInteger and(BigInteger val)  返回两个大整数的按位与的结果
BigInteger andNot(BigInteger val) 返回两个大整数与非的结果
BigInteger divide(BigInteger val)  返回两个大整数的商
double doubleValue()   返回大整数的double类型的值
float floatValue()   返回大整数的float类型的值
BigInteger gcd(BigInteger val)  返回大整数的最大公约数
int intValue() 返回大整数的整型值
long longValue() 返回大整数的long型值
BigInteger max(BigInteger val) 返回两个大整数的最大者
BigInteger min(BigInteger val) 返回两个大整数的最小者
BigInteger mod(BigInteger val) 用当前大整数对val求模
BigInteger multiply(BigInteger val) 返回两个大整数的积
BigInteger negate() 返回当前大整数的相反数
BigInteger not() 返回当前大整数的非
BigInteger or(BigInteger val) 返回两个大整数的按位或
BigInteger pow(int exponent) 返回当前大整数的exponent次方
BigInteger remainder(BigInteger val) 返回当前大整数除以val的余数
BigInteger leftShift(int n) 将当前大整数左移n位后返回
BigInteger rightShift(int n) 将当前大整数右移n位后返回
BigInteger subtract(BigInteger val)返回两个大整数相减的结果
byte[] toByteArray(BigInteger val)将大整数转换成二进制反码保存在byte数组中
String toString() 将当前大整数转换成十进制的字符串形式
BigInteger xor(BigInteger val) 返回两个大整数的异或

作者:这是朕的江山
链接:http://www.jianshu.com/p/8b89ab19db84
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/ctsas/article/details/78811038