bigdecimal java 最大值_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

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值