BigDecimal加减乘除绝对值及比较大小

import java.math.*;
public class BigDecimalTest{
	public static void main(String[] args){
		BigDecimal num1 = new BigDecimal(0.005);
		BigDecimal num2 = new BigDecimal(1000000);
		BigDecimal num3 = new BigDecimal(-10000);

		BigDecimal num12 = new BigDecimal("0.005");
		BigDecimal num22 = new BigDecimal("100000");
		BigDecimal num33 = new BigDecimal("-10000");

		//add 
		BigDecimal result1 = num1.add(num2);
		System.out.println(result1);
		BigDecimal result12 = num12.add(num22);
		System.out.println(result12);

		//subtract
		BigDecimal result2 = num1.subtract(num2);
		System.out.println(result2);
		BigDecimal result22 = num12.subtract(num22);
		System.out.println(result22);

		//multiply
		BigDecimal result3 = num1.multiply(num2);
		System.out.println(result3);
		BigDecimal result33 = num12.multiply(num22);
		System.out.println(result33);

		//divide
		BigDecimal result4 = num2.divide(num1, 20, BigDecimal.ROUND_HALF_UP);
		System.out.println(result4);
		BigDecimal result44 = num22.divide(num12, 20, BigDecimal.ROUND_HALF_UP);
		System.out.println(result44);

		//abs
		BigDecimal result5 = num3.abs();
		System.out.println(result5);
		BigDecimal result52 = num33.abs();
		System.out.println(result52);

		BigDecimal realpay = new BigDecimal("200");
		BigDecimal remainsum = new BigDecimal("100");

		if(remainsum.compareTo(realpay) == -1) {
			//a < b
			System.out.println("the remain is not enough!");
		}
		/*
		if(a.compareTo(b) == 0) ----a=b
		if(a.compareTo(b) == 1) --- a>b
		if(a.compareTo(b) > -1) --- a>=b
		if(a.compareTo(b) < 1) ---- a<=b		
		*/
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值