Java中的数学运算BigDecimal

时间:2023-03-09 13:40:45
Java中的数学运算BigDecimal

Math类

package ch7;

/**
* Created by Jiqing on 2016/11/24.
*/
public class MathDemo {
public static void main(String[] args) {
/* 取整运算 */
// 返回小于目标数的最大整数
System.out.println("Math.floor(-1.2):"+Math.floor(-1.2)); // -2.0 // 返回大于目标数的最小整数
System.out.println("Math.ceil(1.2):"+Math.ceil(1.2)); // 2.0 // 四舍五入取整
System.out.println("Math.round(2.3):" +Math.round(2.3)); // 2 /* 大小相关计算 */
// 找出最大值
System.out.println("Math.max(2.3,4.5):"+Math.max(2.3,4.5)); // 4.5
// 找出最小值
System.out.println("Math.min(2.3,4.5):"+Math.min(2.3,4.5)); // 2.3
// 返回一个随机数0~1之间
System.out.println("Math.random():"+Math.random());
}
}

BigDecimal

package ch7;

import java.math.BigDecimal;

/**
* Created by Jiqing on 2016/11/24.
*/
public class BigDecimalDemo {
public static void main(String[] args) {
// float、double容易精度丢失
System.out.println("0.05 + 0.01 = " + (0.05 + 0.01)); //0.060000000000000005
System.out.println("1.0 - 0.42 = " + (1.0 - 0.42)); //0.5800000000000001
System.out.println("4.015 * 100 = " + (4.015 * 100)); // 401.49999999999994
System.out.println("123.3 / 100 = " + (123.3 / 100)); // 1.2329999999999999 // BigDecimal解决精度问题
BigDecimal f1 = new BigDecimal("0.05"); // 使用String作为构造器参数
BigDecimal f2 = BigDecimal.valueOf(0.01);
BigDecimal f3 = new BigDecimal(0.05); // 使用double作为构造器参数 System.out.println("使用String作为BigDecimal构造器参数:");
System.out.println("0.05 + 0.01 = " + f1.add(f2));
System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
System.out.println("0.05 / 0.01 = " + f1.divide(f2));
//使用String作为BigDecimal构造器参数:
//0.05 + 0.01 = 0.06
//0.05 - 0.01 = 0.04
//0.05 * 0.01 = 0.0005
//0.05 / 0.01 = 5 System.out.println("使用double作为BigDecimal构造器参数:");
System.out.println("0.05 + 0.01 = " + f3.add(f2));
System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
System.out.println("0.05 / 0.01 = " + f3.divide(f2)); //使用double作为BigDecimal构造器参数:
//0.05 + 0.01 = 0.06000000000000000277555756156289135105907917022705078125
//0.05 - 0.01 = 0.04000000000000000277555756156289135105907917022705078125
//0.05 * 0.01 = 0.0005000000000000000277555756156289135105907917022705078125
//0.05 / 0.01 = 5.000000000000000277555756156289135105907917022705078125
}
}

衍生出来的工具类Arith

package ch7;

import java.math.BigDecimal;
import java.util.function.BinaryOperator; /**
* Created by Jiqing on 2016/11/24.
*/
public class Arith {
// 进行精确计算的工具类
// 默认除法运算精度
private static final int DEF_DIV_SCALE = 10;
// 构造器私有,让这个类不能实例化
private Arith() {} // 提供精确的加法运算
public static double add(double v1,double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.add(b2).doubleValue();
} // 提供精确减法运算
public static double sub(double v1,double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.subtract(b2).doubleValue();
} // 提供精确的乘法运算
public static double mul(double v1,double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.multiply(b2).doubleValue();
} // 提供相对精确的除法运算
public static double div(double v1,double v2) {
BigDecimal b1 = BigDecimal.valueOf(v1);
BigDecimal b2 = BigDecimal.valueOf(v2);
return b1.divide(b2,DEF_DIV_SCALE,BigDecimal.ROUND_HALF_UP).doubleValue();
} public static void main(String[] args) {
System.out.println("0.05 + 0.01= " + Arith.add(0.05,0.01)); //0.05 + 0.01= 0.06
System.out.println("1.0 - 0.42= " + Arith.sub(1.0,0.42)); //1.0 - 0.42= 0.58
System.out.println("4.015 * 100= " + Arith.mul(4.015,100)); //4.015 * 100= 401.5
System.out.println("123.3 / 1000= " + Arith.div(123.3,1000)); //123.3 / 1000= 0.1233
}
}