关键技术剖析:
²
²
package prime;
import java.math.BigDecimal;
import java.math.BigInteger;
public class BigNumber {
// 默认除法运算精度,即保留小数点多少位
private static final int DEFAULT_DIV_SCALE = 10;
// 这个类不能实例化
private BigNumber() {
}
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();
}
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();
}
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();
}
public static double div(double v1, double v2) {
return div(v1, v2, DEFAULT_DIV_SCALE);
}
public static double div(double v1, double v2, int scale) {
if (scale < 0) {
System.err.println("除法精度必须大于0!");
return 0;
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return (b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP)).doubleValue();
}
public static BigInteger getFactorial(int n) {
if (n < 0) {
System.err.println("n必须大于等于0!");
return new BigInteger("-1");
} else if (n == 0) {
return new BigInteger("0");
}
// 将数组换成字符串后构造BigInteger
BigInteger result = new BigInteger("1");
for (; n > 0; n--) {
// 将数字n转换成字符串后,再构造一个BigInteger对象,与现有结果做乘法
result = result.multiply(new BigInteger(new Integer(n).toString()));
}
return result;
}
public static void main(String[] args) {
// 如果我们编译运行下面这个程序会看到什么?
System.out.println(0.05 + 0.01);
System.out.println(1.0 - 0.42);
System.out.println(4.015 * 100);
System.out.println(123.3 / 100);
// 0.060000000000000005
// 0.5800000000000001
// 401.49999999999994
// 1.2329999999999999
// 计算阶乘,可以将n设得更大
int n = 30;
System.out.println("计算n的阶乘" + n + "! = " + BigNumber.getFactorial(n));
// 用double构造BigDecimal
BigDecimal bd1 = new BigDecimal(0.1);
System.out.println("(bd1 = new BigDecimal(0.1)) = " + bd1.toString());
// 用String构造BigDecimal
BigDecimal bd2 = new BigDecimal("0.1");
System.out.println("(bd2 = new BigDecimal('0.1')) = " + bd2.toString());
BigDecimal bd3 = new BigDecimal("0.10");
// equals方法比较两个BigDecimal对象是否相等,相等返回true,不等返回false
System.out.println("bd2.equals(bd3) = " + bd2.equals(bd3));// false
// compareTo方法比较两个BigDecimal对象的大小,相等返回0,小于返回-1,大于返回1。
System.out.println("bd2.compareTo(bd3) = " + bd2.compareTo(bd3));// 0
// 进行精确计算
System.out.println("0.05 + 0.01 = " + BigNumber.add(0.05, 0.01));
System.out.println("1.0 - 0.42 = " + BigNumber.add(1.0, 0.42));
System.out.println("4.015 * 100 =" + BigNumber.add(4.015, 100));
System.out.println("123.3 / 100 = " + BigNumber.add(123.3, 100));
}
}
运行结果:
0.060000000000000005
0.5800000000000001
401.49999999999994
1.2329999999999999
计算n的阶乘30! = 265252859812191058636308
(bd1 = new BigDecimal(0.1)) = 0.100000000000000005551115
(bd2 = new BigDecimal("0.1")) = 0.1
bd2.equals(bd3) = false
bd2.compareTo(bd3) = 0
0.05 + 0.01 = 0.06
1.0 - 0.42 = 1.42
4.015 * 100 =104.015
123.3 / 100 = 223.3
源码分析:
²
²
²
²