这篇帖子通过实例展示了如何在java中将BigInteger与他人进行分割。
但对于BigInteger对象来说,同样的操作并不适用,它给出了编译错误操作符'/'不能应用于'java.math.BigInteger', 'java.math.BigInteger'。
下面是一个使用除法运算符的例子
import java.math.BigInteger;
public class BigIntegerMultiply {
public static void main(String[] args) {
BigInteger operand1= BigInteger.valueOf(2);
BigInteger operand2= BigInteger.valueOf(2);
BigInteger result=operand1*operand2;
System.out.println(result);
}
}
它提供了一个multiply 方法来对BigInteger进行乘法运算。
Java BigInteger的乘法方法及示例
divide 方法返回数值乘法的结果。
BigInteger是不可改变的,方法的结果总是返回一个新对象。
它在内部使用一个整数数组来完成这一过程,与整数乘法相比,其性能较差。
语法:
public BigInteger divide(BigInteger val)
下面是一个BigInteger除法的例子
import java.math.BigInteger;
public class BigIntegerMultiply {
public static void main(String[] args) {
BigInteger divident= BigInteger.valueOf(212312);
BigInteger divisor= BigInteger.valueOf(2123);
BigInteger result=divident.divide(divisor);
System.out.println(result);
}
}
输出:
100
将两个BigDecimals值分割成一个BigDecimal对象
首先,使用下面的代码将BigInteger转换成BigDecimal 接下来,使用除法,结果是BigDecimal。
import java.math.BigDecimal;
import java.math.RoundingMode;
public class BigIntegerMultiply {
public static void main(String[] args) {
BigDecimal operand1= BigDecimal.valueOf(212312);
BigDecimal operand2= BigDecimal.valueOf(2123);
BigDecimal result=operand1.divide(operand2,4, RoundingMode.HALF_UP);
System.out.println(result);
}
}
输出:
100.0057