直接上操作,BIgInteger import java.math.BigInteger; public class Main { public static void main(String[] args) { BigInteger big1 = new BigInteger("4321342342342342"); BigInteger big2 = new BigInteger("6435634324223"); System.out.println("加法操作:" + big1.add(big2)); System.out.println("减法操作:" + big1.subtract(big2)); System.out.println("乘法操作:" + big1.multiply(big2)); System.out.println("除法操作:" + big1.divide(big2)); System.out.println("次方运算:" + big1.pow(2)); // 2次方 BigInteger[] res = big1.divideAndRemainder(big2); // 求余 System.out.println("商:" + res[0] + "、余:" + res[1]); } }
如果在计算中不超过数据类型的长度,不建议使用大数字类计算,因为这种方法的性能很低。
BigDecimal可以进行小数运算。
package Demo_2_3_BigDeciaml; import java.math.BigDecimal; import java.math.BigInteger; public class Main { public static void main(String[] args) { BigDecimal big1 = new BigDecimal("123123124345346.213"); BigDecimal big2 = new BigDecimal("524576345252.542"); System.out.println("加法操作:" + big1.add(big2)); System.out.println("减法操作:" + big1.subtract(big2)); System.out.println("乘法操作:" + big1.multiply(big2)); System.out.println("次方运算:" + big1.pow(2)); // 2次方 BigDecimal[] res = big1.divideAndRemainder(big2); // 求余 System.out.println("商:" + res[0] + "、余:" + res[1]); } }