Dowemo
0 0 0 0

1import java.math.BigDecimal; 2importstatic java.lang.System.out; 3publicclass BaseClass { 4publicstaticvoid main(String[] args) { 5 BigDecimal num1 = new BigDecimal("100");  6 BigDecimal num2 = new BigDecimal("50");  7 8//加法 9 BigDecimal result1 = num1.add(num2);10 out.println("和 是 :" + result1);1112//减法 13 BigDecimal result2 = num1.subtract(num2);14 out.println("差 是 :" + result2);1516//乘法17 BigDecimal result3 = num1.multiply(num2);18 out.println("积 是 :" + result3);1920//除法21 BigDecimal result4 = num1.divide(num2);22 out.println("商 是 :" + result4);2324//比较25 BigDecimal num = new BigDecimal("100"); //用做比较的值 26 out.println(num1.compareTo(num2)); //大于 时,返回 1 27 out.println(num.compareTo(num1)); //等于 时,返回 0 28 out.println(num2.compareTo(num1)); //小于 时,返回 -12930 out.println(num1.min(num2)); //求两个数的最小值,返回小的数31 out.println(num1.max(num2)); //求两个数的最大值,返回大的数3233// compareTo方法比较时,不同于equals方法,需要两边都不为null,否则会报空指针异常,34// 源码如下35// public int compareTo(BigDecimal val) {36//// Quick path for equal scale and non-inflated case.37// if (scale == val.scale) {38// long xs = intCompact;39// long ys = val.intCompact;40// if (xs!= INFLATED && ys!= INFLATED)41// return xs!= ys? ((xs> ys)? 1 : -1) : 0;42// }43// int xsign = this.signum();44// int ysign = val.signum();45// if (xsign!= ysign)46// return (xsign> ysign)? 1 : -1;47// if (xsign == 0)48// return 0;49// int cmp = compareMagnitude(val);50// return (xsign> 0)? cmp : -cmp;51// }5253// max/min方法源代码如下54// public BigDecimal max(BigDecimal val) {55// return (compareTo(val)> = 0? this : val);56// }57// public BigDecimal min(BigDecimal val) {58// return (compareTo(val) <= 0? this : val);59// }60 }61 }



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs