java 计算工具类_Java精确计算工具类及数字转换百分比

packagecom.JohanChan.utils;importjava.math.BigDecimal;public classCalculateUtils {/*** 加法

*

*@paramvar1

*@paramvar2

*@return

*/

public static double add(double var1, doublevar2) {

BigDecimal b1= newBigDecimal(Double.toString(var1));

BigDecimal b2= newBigDecimal(Double.toString(var2));returnb1.add(b2).doubleValue();

}/*** 减法

*

*@paramvar1

*@paramvar2

*@return

*/

public static double sub(double var1, doublevar2) {

BigDecimal b1= newBigDecimal(Double.toString(var1));

BigDecimal b2= newBigDecimal(Double.toString(var2));returnb1.subtract(b2).doubleValue();

}/*** 乘法

*

*@paramvar1

*@paramvar2

*@return

*/

public static double mul(double var1, doublevar2) {

BigDecimal b1= newBigDecimal(Double.toString(var1));

BigDecimal b2= newBigDecimal(Double.toString(var2));returnb1.multiply(b2).doubleValue();

}/*** 除法

*

*@paramv1 被除数

*@paramv2 除数

*@paramscale 精度,到小数点后几位

*@return

*/

public static double div(double v1, double v2, intscale) {if (scale < 0) {throw new IllegalArgumentException("精确度不能小于0");

}

BigDecimal b1= newBigDecimal(Double.toString(v1));

BigDecimal b2= newBigDecimal(Double.toString(v2));returnb1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();

}

/**

* 四舍五入

* @param v

* @param scale 精确位数

* @return

*/

public static double round(double v, int scale) {

if (scale < 0) {

throw new IllegalArgumentException("精确度不能小于0");

}

BigDecimal b = new BigDecimal(Double.toString(v));

return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

// return b.divide(b, scale, BigDecimal.ROUND_HALF_UP).doubleValue();

}

/*

* 加法计算并保留小数点

* */

public static double addRound(double var1, double var2, int scale) {

if (scale < 0) {

throw new IllegalArgumentException("精确度不能小于0");

}

return round(add(var1,var2),scale);

// BigDecimal b = new BigDecimal(Double.toString(add(var1,var2)));

// return b.divide(b, scale, BigDecimal.ROUND_HALF_UP).doubleValue();

}

/*

* 减法计算并保留小数点

* */

public static double subRound(double var1, double var2, int scale) {

if (scale < 0) {

throw new IllegalArgumentException("精确度不能小于0");

}

return round(sub(var1,var2),scale);

}

/*

* 乘法计算并保留小数点

* */

public static double mulRound(double var1, double var2, int scale) {

if (scale < 0) {

throw new IllegalArgumentException("精确度不能小于0");

}

return round(mul(var1,var2),scale);

}

//把数字转为指定格式的百分数

public static String getPercentFormat(int maxFract, int minFract,int maxInt,int minInt,Double d){

NumberFormat percentFormat = NumberFormat.getPercentInstance();

percentFormat.setMaximumFractionDigits(maxFract); //最大小数位数

percentFormat.setMaximumIntegerDigits(maxInt);//最大整数位数

percentFormat.setMinimumFractionDigits(minFract); //最小小数位数

percentFormat.setMinimumIntegerDigits(minInt);//最小整数位数

return percentFormat.format(d);//自动转换成百分比显示

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值