import java.math.BigInteger;
public class BigIntegerExample {
public static void main(String[] args) {
// 创建两个BigInteger对象
BigInteger num1 = new BigInteger("123456789012345678901234567890");
BigInteger num2 = new BigInteger("987654321098765432109876543210");
// 加法操作
BigInteger sum = num1.add(num2);
System.out.println("num1 + num2 = " + sum);
// 减法操作
BigInteger difference = num1.subtract(num2);
System.out.println("num1 - num2 = " + difference);
// 乘法操作
BigInteger product = num1.multiply(num2);
System.out.println("num1 * num2 = " + product);
// 除法操作
BigInteger quotient = num1.divide(num2);
System.out.println("num1 / num2 = " + quotient);
// 取模操作
BigInteger remainder = num1.mod(num2);
System.out.println("num1 % num2 = " + remainder);
// 比较大小
int comparison = num1.compareTo(num2);
if (comparison > 0) {
System.out.println("num1 is greater than num2");
} else if (comparison < 0) {
System.out.println("num1 is less than num2");
} else {
System.out.println("num1 is equal to num2");
}
}
}
BigInteger
是 Java 中的一个类,用于表示任意精度的整数。它可以处理超出基本数据类型(如 int
或 long
)范围的数值。new BigInteger(String val)
:通过字符串创建一个 BigInteger
对象。add(BigInteger val)
:返回两个 BigInteger
的和。subtract(BigInteger val)
:返回两个 BigInteger
的差。multiply(BigInteger val)
:返回两个 BigInteger
的积。divide(BigInteger val)
:返回两个 BigInteger
的商。mod(BigInteger val)
:返回两个 BigInteger
的余数。compareTo(BigInteger val)
:比较两个 BigInteger
的大小,返回值为 -1
、0
或 1
,分别表示小于、等于或大于。Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站