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
*/
}
}
BigDecimal加减乘除绝对值及比较大小
最新推荐文章于 2024-02-05 17:10:30 发布