一、BigInteger类
/**
* BigInteger
* 不可变的任意精度的整数。
* 所有操作中,都以二进制补码形式表示 BigInteger(如 Java 的基本整数类型)。
* BigInteger 提供所有 Java 的基本整数操作符的对应物,并提供 java.lang.Math 的所有相关方法。
* 另外,BigInteger 还提供以下运算:模算术、GCD 计算、质数测试、素数生成、位操作以及一些其他操作。
* 算术运算的语义完全模仿 Java 整数算术运算符的语义
* 位移操作的语义扩展了 Java 的位移操作符的语义以允许产生负位移距离。带有负位移距离的右移操作会导致左移操作,反之亦然。忽略无符号的右位移运算符(>>>)
* 提供的模算术操作用来计算余数、求幂和乘法可逆元
*/
public class BigInteger extends Number implements Comparable<BigInteger>
{
//构造方法
/**
* 将 BigInteger 的十进制字符串表示形式转换为 BigInteger。
* 该字符串表示形式包括一个可选的减号,后跟一个或多个十进制数字序列。
* 字符到数字的映射由 Character.digit 提供。该字符串不能包含任何其他字符(例如,空格)。
*/
public BigInteger(String val){}
public BigInteger(String val,
int radix){}//radix指定基数
//常用方法
//返回其值为 (this + val) 的 BigInteger。
public BigInteger add(BigInteger val){}
//返回其值为 (this - val) 的 BigInteger。
public BigInteger subtract(BigInteger val){}
//返回其值为 (this * val) 的 BigInteger。
public BigInteger multiply(BigInteger val){}
//返回其值为 (this / val) 的 BigInteger。
public BigInteger divide(BigInteger val){}
//返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。 即返回商和余数
public BigInteger[] divideAndRemainder(BigInteger val){}
}
二、示例
import java.math.*;
class BigIntegerDemo
{
public static void main(String[] args)
{
String num1 = "2135484";
String num2 = "0005";
System.out.println(num1+" + "+num2+" = "+add(num1,num2));
System.out.println(num1+" - "+num2+" = "+sub(num1,num2));
System.out.println(num1+" * "+num2+" = "+mul(num1,num2));
BigInteger[] bis = div(num1,num2);
System.out.println(num1+" / "+num2+" = "+bis[0]+"余:"+bis[1]);
}
//加
public static String add(String num1,String num2)
{
BigInteger bi1 = new BigInteger(num1);
BigInteger bi2 = new BigInteger(num2);
return bi1.add(bi2).toString();
}
//减
public static String sub(String num1,String num2)
{
BigInteger bi1 = new BigInteger(num1);
BigInteger bi2 = new BigInteger(num2);
return bi1.subtract(bi2).toString();
}
//乘
public static String mul(String num1,String num2)
{
BigInteger bi1 = new BigInteger(num1);
BigInteger bi2 = new BigInteger(num2);
return bi1.multiply(bi2).toString();
}
//除
public static BigInteger[] div(String num1,String num2)
{
BigInteger bi1 = new BigInteger(num1);
BigInteger bi2 = new BigInteger(num2);
return bi1.divideAndRemainder(bi2);
}
}