1、加法
@Test
public void testAdd() {
//用double类型初始化BigDecimal对象
BigDecimal numA = new BigDecimal(0.05);
BigDecimal numB = new BigDecimal(0.06);
System.out.println("numA + numB = " + numA.add(numB));
//用double类型和int类型初始化BigDecimal对象。(作加法运算时得到的只是一个近似值)
BigDecimal numC = new BigDecimal(3.05);
BigDecimal numD = new BigDecimal(100);
System.out.println("numC + numD = " + numC.add(numD));
//用字符串类型初始化BigDecimal对象。(作加法运算时得到的是精确值)
BigDecimal strA = new BigDecimal("3.05");
BigDecimal strB = new BigDecimal("100");
System.out.println("strA + strB = " + strA.add(strB));
}
打印的结果如下:
numA + numB = 0.11000000000000000055511151231257827021181583404541015625
numC + numD = 103.04999999999999982236431605997495353221893310546875
strA + strB = 103.05
2、减法
@Test
public void testSubtract() {
//用double类型初始化BigDecimal对象
BigDecimal numA = new BigDecimal(0.05);
BigDecimal numB = new BigDecimal(0.06);
System.out.println("numA + numB = " + numA.subtract(numB));
//用double类型和int类型初始化BigDecimal对象。(作减法运算时得到的只是一个近似值)
BigDecimal numC = new BigDecimal(100);
BigDecimal numD = new BigDecimal(0.05);
System.out.println("numC + numD = " + numC.subtract(numD));
//用字符串类型初始化BigDecimal对象。(作减法运算时得到的是精确值)
BigDecimal strA = new BigDecimal("100");
BigDecimal strB = new BigDecimal("0.05");
System.out.println("strA + strB = " + strA.subtract(strB));
}
打印结果如下:文章来源:https://www.toymoban.com/news/detail-508268.html
numA + numB = -0.00999999999999999500399638918679556809365749359130859375
numC + numD = 99.94999999999999999722444243843710864894092082977294921875
strA + strB = 99.95
3、乘法
@Test
public void testMultiply() {
//用double类型初始化BigDecimal对象
BigDecimal numA = new BigDecimal(0.05);
BigDecimal numB = new BigDecimal(0.06);
System.out.println("numA + numB = " + numA.multiply(numB));
//用double类型和int类型初始化BigDecimal对象。(作乘法运算时得到的只是一个近似值)
BigDecimal numC = new BigDecimal(100);
BigDecimal numD = new BigDecimal(0.05);
System.out.println("numC + numD = " + numC.multiply(numD));
//用字符串类型初始化BigDecimal对象。(作乘法运算时得到的是精确值)
BigDecimal strA = new BigDecimal("100");
BigDecimal strB = new BigDecimal("0.05");
System.out.println("strA + strB = " + strA.multiply(strB));
}
打印结果如下:文章来源地址https://www.toymoban.com/news/detail-508268.html
numA + numB = 0.00300000000000000005551115123125782085820576136538628584587058372823258067807472571075777523219585418701171875
numC + numD = 5.00000000000000027755575615628913510590791702270507812500
strA + strB = 5.00
4、除法
@Test
public void testDivide() {
BigDecimal numA = new BigDecimal("1");
BigDecimal numB = new BigDecimal("-1");
BigDecimal numC = new BigDecimal("3");
// 保留两位小数,舍入模式为UP
System.out.println("1/3保留两位小数(UP) = " + numA.divide(numC, 2, RoundingMode.UP));
System.out.println("-1/3保留两位小数(UP) = " + numB.divide(numC, 2, RoundingMode.UP));
// 保留两位小数,舍入模式为DOWN
System.out.println("1/3保留两位小数(DOWN) = " + numA.divide(numC, 2, RoundingMode.DOWN));
System.out.println("-1/3保留两位小数(DOWN) = " + numB.divide(numC, 2, RoundingMode.DOWN));
// 保留两位小数,舍入模式为CEILING
System.out.println("1/3保留两位小数(CEILING) = " + numA.divide(numC, 2, RoundingMode.CEILING));
System.out.println("-1/3保留两位小数(CEILING) = " + numB.divide(numC, 2, RoundingMode.CEILING));
// 保留两位小数,舍入模式为FLOOR
System.out.println("1/3保留两位小数(FLOOR) = " + numA.divide(numC, 2, RoundingMode.FLOOR));
System.out.println("-1/3保留两位小数(FLOOR) = " + numB.divide(numC, 2, RoundingMode.FLOOR));
BigDecimal numD = new BigDecimal("1");
BigDecimal numE = new BigDecimal("-1");
BigDecimal numF = new BigDecimal("8");
// 保留两位小数,舍入模式为HALF_UP
System.out.println("1/8(=0.125)保留两位小数(HALF_UP) = " + numD.divide(numF, 2, RoundingMode.HALF_UP));
System.out.println("-1/8(=0.125)保留两位小数(HALF_UP) = " + numE.divide(numF, 2, RoundingMode.HALF_UP));
// 保留两位小数,舍入模式为HALF_DOWN
System.out.println("1/8(=0.125)保留两位小数(HALF_DOWN) = " + numD.divide(numF, 2, RoundingMode.HALF_DOWN));
System.out.println("-1/8(=0.125)保留两位小数(HALF_DOWN) = " + numE.divide(numF, 2, RoundingMode.HALF_DOWN));
// 保留两位小数,舍入模式为HALF_EVEN
System.out.println("0.54/4(=0.135)保留两位小数(HALF_EVEN) = " + new BigDecimal("0.54").divide(new BigDecimal("4"), 2, RoundingMode.HALF_EVEN));
System.out.println("1/8(=0.125)保留两位小数(HALF_EVEN) = " + numE.divide(numF, 2, RoundingMode.HALF_EVEN));
//UNNECESSARY,会报异常
System.out.println("1/8(=0.125) = " + numE.divide(numF, RoundingMode.UNNECESSARY));
}
打印结果:
1/3保留两位小数(UP) = 0.34
-1/3保留两位小数(UP) = -0.34
1/3保留两位小数(DOWN) = 0.33
-1/3保留两位小数(DOWN) = -0.33
1/3保留两位小数(CEILING) = 0.34
-1/3保留两位小数(CEILING) = -0.33
1/3保留两位小数(FLOOR) = 0.33
-1/3保留两位小数(FLOOR) = -0.34
1/8(=0.125)保留两位小数(HALF_UP) = 0.13
-1/8(=0.125)保留两位小数(HALF_UP) = -0.13
1/8(=0.125)保留两位小数(HALF_DOWN) = 0.12
-1/8(=0.125)保留两位小数(HALF_DOWN) = -0.12
0.54/4(=0.135)保留两位小数(HALF_EVEN) = 0.14
1/8(=0.125)保留两位小数(HALF_EVEN) = -0.12
5.绝对值
@Test
public void testAbs() {
BigDecimal a = new BigDecimal("1.234");
BigDecimal b = new BigDecimal("-1.234");
System.out.println("1.234的绝对值:" + a.abs());
System.out.println("-1.234的绝对值:" + b.abs());
System.out.println("-1.234的绝对值,保留两位有效数字:" + b.abs(new MathContext(2)));
}
打印结果为:
1.234的绝对值:1.234
-1.234的绝对值:1.234
-1.234的绝对值,保留两位有效数字:1.2
到了这里,关于bigdecimal加减乘除运算的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!