public static double add(double v1,double v2)
public static double sub(double v1,double v2)
public static double mul(double v1,double v2)
public static double div(double v1,double v2)
public static double div(double v1,double v2,int scale)
public static double round(double v,int scale)
[java] view plaincopy importjava.math.BigDecimal;
public class Arith{//默认除法运算精度
private static final int DEF_DIV_SCALE = 10;//这个类不能实例化 privateArith(){ }
public static double add(double v1,doublev2){ BigDecimal b1 = newBigDecimal(Double.toString(v1)); BigDecimal b2 = newBigDecimal(Double.toString(v2)); returnb1.add(b2).doubleValue(); }
public static double sub(double v1,doublev2){ BigDecimal b1 = newBigDecimal(Double.toString(v1)); BigDecimal b2 = newBigDecimal(Double.toString(v2)); returnb1.subtract(b2).doubleValue(); }
public static double mul(double v1,doublev2){ BigDecimal b1 = newBigDecimal(Double.toString(v1)); BigDecimal b2 = newBigDecimal(Double.toString(v2)); returnb1.multiply(b2).doubleValue(); }
public static double div(double v1,doublev2){ returndiv(v1,v2,DEF_DIV_SCALE); }
public static double div(double v1,doublev2,intscale){ if(scale<0){ throw newIllegalArgumentException( "The scale must be a positive integer orzero"); } BigDecimal b1 = newBigDecimal(Double.toString(v1)); BigDecimal b2 = newBigDecimal(Double.toString(v2)); returnb2.divide(b1,scale,BigDecimal.ROUND_HALF_UP).doubleValue(); }
publicstatic double round(double v,intscale){ if(scale<0){ throw new IllegalArgumentException("The scale must be a positiveinteger orzero"); } BigDecimal b = newBigDecimal(Double.toString(v)); BigDecimal one = newBigDecimal("1"); returnb.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue(); } };
(2)
import java.math.BigDecimal;
importstaticjava.lang.System.out;
public class BaseClass{
public static void main(String[] args){
BigDecimal num1 = new BigDecimal("100");
BigDecimal num2 = new BigDecimal("50");
//加法
BigDecimal result1 =num1.add(num2);
//减法
BigDecimal result2 =num1.subtract(num2);
//乘法
BigDecimal result3 =num1.multiply(num2);
//除法
BigDecimal result4 =num1.divide(num2);
//比较
BigDecimal num = newBigDecimal("100"); //用做比较的值 out.println(num1.compareTo(num2)); //大于 时,返回 1 out.println(num.compareTo(num1)); //等于 时,返回 0 out.println(num2.compareTo(num1)); //小于 时,返回-1 out.println(num1.min(num2)); //求两个数的最小值,返回小的数 out.println(num1.max(num2)); //求两个数的最大值,返回大的数 // compareTo方法比较时,不同于equals方法,需要两边都不为null,否则会报空指针异常,//
(源码)
源码如下//
public int compareTo(BigDecimal val){//
// Quick path for equal scale and non-inflatedcase.
// if (scale == val.scale) {
// long xs = intCompact;
// long ys =val.intCompact;
// if (xs != INFLATED&& ys != INFLATED)
//return xs != ys ? ((xs > ys) ? 1 : -1) : 0;
// }
// int xsign = this.signum();
// int ysign = val.signum();
//if (xsign != ysign)
// return (xsign > ysign) ? 1 : -1;
// if (xsign == 0)
// return 0;
// int cmp = compareMagnitude(val);
// return (xsign > 0) ? cmp : -cmp;
// }
// max/min方法源代码如下
// public BigDecimal max(BigDecimal val) {
// return (compareTo(val) >= 0 ? this : val);
// }
// public BigDecimal min(BigDecimal val) {
// return (compareTo(val) <= 0 ? this : val);
// }
BigDecimal
最新推荐文章于 2023-06-22 15:47:04 发布