高精度的大数字运算

package com.test.util;

import java.math.BigDecimal;
/**
 *
 * @ClassName: NumberOperator
 *
 * @Description: 两数相加乘除
 * @date 2015年3月2日 下午1:35:58
 * @version
 */
public class BigDecimalOperation {
    
    /**
     *
     * @Title: add  
     * @Description: 提供精确的加法运算
     * @param d1 加数
     * @param d2 被加数
     * @return double
     * @throws
     */
    public static double add(double d1, double d2) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.add(b2).doubleValue();
    }
    
    /**
     *
     * @Title: addStr  
     * @Description: 提供精确的加法运算
     * @param d1 加数
     * @param d2 被加数
     * @return String
     * @throws
     */
    public static String addStr(double d1, double d2) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.add(b2).toString();
    }
    
    /**
     *
     * @Title: add  
     * @Description: 提供精确的加法运算
     * @param s1 加数
     * @param s2 被加数
     * @return String
     * @throws
     */
    public static String add(String s1, String s2) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.add(b2).toString();
    }
    
    /**
     *
     * @Title: strAdd  
     * @Description: 提供精确的加法运算
     * @param s1 加数
     * @param s2 被加数
     * @return double
     * @throws
     */
    public static double strAdd(String s1, String s2) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.add(b2).doubleValue();
    }
    
    /**
     *
     * @Title: add  
     * @Description: 提供精确的加法运算,并精确到小数后X位
     * @param d1 加数
     * @param d2 被加数
     * @param scale 保留小数后X位
     * @return double
     * @throws
     */
    public static double add(double d1, double d2, int scale) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.add(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
    /**
     *
     * @Title: addStr
     * @Description: 提供精确的加法运算,并精确到小数后X位
     * @param d1 加数
     * @param d2 被加数
     * @param scale 保留小数后X位
     * @return String
     * @throws
     */
    public static String addStr(double d1, double d2, int scale) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.add(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }
    
    /**
     *
     * @Title: add
     * @Description: 提供精确的加法运算,并精确到小数后X位
     * @param s1 加数
     * @param s2 被加数
     * @param scale 保留小数后X位
     * @return double
     * @throws
     */
    public static String add(String s1, String s2, int scale) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.add(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }
    
    /**
     *
     * @Title: strAdd
     * @Description: 提供精确的加法运算,并精确到小数后X位
     * @param s1 加数
     * @param s2 被加数
     * @param scale 保留小数后X位
     * @return double
     * @throws
     */
    public static double strAdd(String s1, String s2, int scale) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.add(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
    /**
     *
     * @Title: sub  
     * @Description: 提供精确的减法运算
     * @param d1 减数
     * @param d2 被减数
     * @return double
     * @throws
     */
    public static double sub(double d1, double d2) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.subtract(b2).doubleValue();
    }
    
    /**
     *
     * @Title: strSub  
     * @Description: 提供精确的减法运算
     * @param d1 间数
     * @param d2 被减数
     * @return String
     * @throws
     */
    public static String strSub(double d1, double d2) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.subtract(b2).toString();
    }
    
    /**
     *
     * @Title: sub  
     * @Description: 提供精确的减法运算
     * @param s1 减数
     * @param s2 被减数
     * @return double
     * @throws
     */
    public static double sub(String s1, String s2) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.subtract(b2).doubleValue();
    }    
    
    /**
     *
     * @Title: strSub  
     * @Description: 提供精确的减法运算
     * @param s1 间数
     * @param s2 被减数
     * @return String
     * @throws
     */
    public static String strSub(String s1, String s2) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.subtract(b2).toString();
    }
    
    /**
     *
     * @Title: sub  
     * @Description: 提供精确的减法运算,保留小数后X位
     * @param d1 减数
     * @param d2 被减数
     * @param scale 保留小数后X位
     * @return double
     * @throws
     */
    public static double sub(double d1, double d2, int scale) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.subtract(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
    /**
     *
     * @Title: strSub  
     * @Description: 提供精确的减法运算,保留小数后X位
     * @param d1 减数
     * @param d2 被减数
     * @param scale 保留小数后X位
     * @return String
     * @throws
     */
    public static String strSub(double d1, double d2, int scale) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.subtract(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }
    
    /**
     *
     * @Title: sub  
     * @Description: 提供精确的减法运算,保留小数后X位
     * @param s1 减数
     * @param s2 被减数
     * @param scale 保留小数后X位
     * @return double
     * @throws
     */
    public static double sub(String s1, String s2, int scale) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.subtract(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }    
    
    /**
     *
     * @Title: strSub  
     * @Description: 提供精确的减法运算,保留小数后X位
     * @param s1 减数
     * @param s2 被减数
     * @param scale 保留小数后X位
     * @return String
     * @throws
     */
    public static String strSub(String s1, String s2, int scale) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.subtract(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     *
     * @Title: mul
     * @Description: 提供精确的乘法运算
     * @param d1 乘数
     * @param d2 被乘除
     * @return double
     * @throws
     */
    public static double mul(double d1, double d2) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.multiply(b2).doubleValue();
    }
    
    /**
     *
     * @Title: strMul  
     * @Description: 提供精确的乘法运算
     * @param d1 乘数
     * @param d2 被乘数
     * @return String
     * @throws
     */
    public static String strMul(double d1, double d2) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.multiply(b2).toString();
    }    
    
    /**
     *
     * @Title: mul  
     * @Description: 提供精确的乘法运算
     * @param s1 乘数
     * @param s2 被乘数
     * @return double
     * @throws
     */
    public static double mul(String s1, String s2) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.multiply(b2).doubleValue();
    }
    
    /**
     *
     * @Title: mulStr  
     * @Description: 提供精确的乘法运算
     * @param s1 乘数
     * @param s2 被乘数
     * @return String
     * @throws
     */
    public static String mulStr(String s1, String s2) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.multiply(b2).toString();
    }
    
    /**
     *
     * @Title: mul  
     * @Description: 提供精确的乘法运算,并保留小数后X位
     * @param d1 乘数
     * @param d2 被乘数
     * @param scale 保留小数后X位
     * @return double
     * @throws
     */
    public static double multiply(double d1, double d2, int scale) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
    /**
     *
     * @Title: strMul  
     * @Description: 提供精确的乘法运算,并保留小数后X位
     * @param d1 乘数
     * @param d2 被乘数
     * @param scale 保留小数后X位
     * @return String
     * @throws
     */
    public static String strMul(double d1, double d2, int scale) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }    
    
    /**
     *
     * @Title: strMul  
     * @Description: 提供精确的乘法运算,并保留小数后X位
     * @param s1 乘数
     * @param s2 被乘数
     * @param scale 保留小数后X位
     * @return String
     * @throws
     */
    public static double mul(String s1, String s2, int scale) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
    /**
     *
     * @Title: mulStr  
     * @Description: 提供精确的乘法运算,并保留小数后X位
     * @param s1 乘数
     * @param s2 被乘数
     * @param scale 保留小数后X位
     * @return String
     * @throws
     */
    public static String mulStr(String s1, String s2, int scale) {
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }
    
    /**
     *
     * @Title: div  
     * @Description: 提供(相对)精确的除法运算,当发生除不尽的时候,
     *  精确到scale参数指定精度,以后的数字四舍五入
     *
     * @param d1 除数
     * @param d2 被除数
     * @param scale 保留小数后X位
     * @return double
     * @throws
     */
    public static double div(double d1, double d2, int scale) {
        if (d2 == 0) {
            throw new IllegalArgumentException("The dividend cannot be zero");
        }
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
    /**
     *
     * @Title: strDiv  
     * @Description: 提供(相对)精确的除法运算,当发生除不尽的时候,
     *  精确到scale参数指定精度,以后的数字四舍五入
     * @param d1 除数
     * @param d2 被除数
     * @param scale 保留小数后X位
     * @return String
     * @throws
     */
    public static String strDiv(double d1, double d2, int scale) {
        if (d2 == 0) {
            throw new IllegalArgumentException("The dividend cannot be zero");
        }
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();
    }
    
    /**
     *
     * @Title: div  
     * @Description: 提供(相对)精确的除法运算,当发生除不尽的时候,
     *  精确到scale参数指定精度,以后的数字四舍五入
     * @param s1 除数
     * @param s2 被除数
     * @param scale 保留小数后X位
     * @return double
     * @throws
     */
    public static double div(String s1, String s2, int scale) {
        if (Double.parseDouble(s2) == 0) {
            throw new IllegalArgumentException("The dividend cannot be zero");
        }
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
    /**
     *
     * @Title: divStr  
     * @Description: 提供(相对)精确的除法运算,当发生除不尽的时候,
     *  精确到scale参数指定精度,以后的数字四舍五入
     * @param s1 除数
     * @param s2 被除数
     * @param scale 保留小数后X位
     * @return String
     * @throws
     */
    public static String divStr(String s1, String s2, int scale) {
        if (Double.parseDouble(s2) == 0) {
            throw new IllegalArgumentException("The dividend cannot be zero");
        }
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();
    }
    
    /**
     *
     * @Title: round  
     * @Description: 提供精确的四舍五入处理
     * @param d 需要四舍五入的数字
     * @param scale 保留小数后X位
     * @return double
     * @throws
     */
    public static double round(double d, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(d);
        BigDecimal b2 = new BigDecimal("1");
        return b.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     *
     * @Title: strRound  
     * @Description: 提供精确的四舍五入处理
     * @param d 需要四舍五入的数字
     * @param scale 保留小数后X位
     * @return String
     * @throws
     */
    public static String strRound(double d, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(d);
        BigDecimal b2 = new BigDecimal("1");
        return b.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();
    }
    
    /**
     *
     * @Title: round  
     * @Description: 提供精确的四舍五入处理
     * @param d 需要四舍五入的数字
     * @param scale 保留小数后X位
     * @return double
     * @throws
     */
    public static double round(String d, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(d);
        BigDecimal b2 = new BigDecimal("1");
        return b.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    
    /**
     *
     * @Title: roundStr  
     * @Description: 提供精确的四舍五入处理
     * @param d 需要四舍五入的数字
     * @param scale 保留小数后X位
     * @return String
     * @throws
     */
    public static String roundStr(String d, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(d);
        BigDecimal b2 = new BigDecimal("1");
        return b.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     *
     * @Title: numToStr  
     * @Description: 转成String 防止使用科学计数法
     * @param bd 要格式化的对象
     * @return String
     * @throws
     */
    public static String numToStr(BigDecimal bd) {
        Double d = Double.valueOf(bd.doubleValue());
        return numToStr(d);
    }
    
    /**
     *
     * @Title: numToStr  
     * @Description: 转成String 防止使用科学计数法
     * @param d 要格式化的对象
     * @return String
     * @throws
     */
    public static String numToStr(Double d) {
        java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        return nf.format(d);
    }
    
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值