1 public class Test4 { 2 public static void main(String[] args) { 3 BigInteger num1=new BigInteger("123456789123456789123456789"); 4 BigInteger num2=new BigInteger("2345678901234567890"); 5 System.out.println("加:"+(num1.add(num2))); 6 System.out.println("减:"+(num1.subtract(num2))); 7 System.out.println("乘:"+(num1.multiply(num2))); 8 System.out.println("除:"+(num1.divide(num2))); 9 System.out.println("模:"+(num1.remainder(num2))); 10 BigInteger[] num3=(num1.divideAndRemainder(num2)); 11 for (BigInteger big: num3) { 12 System.out.println("商和余数各为"+big); 13 } 14 15 BigDecimal num4=new BigDecimal("999999999999999999999999999999999999.999999999999999999"); 16 BigDecimal num5=new BigDecimal("8999999999999999999999999999999999999999999.999999999999"); 17 18 System.out.println("和:"+(num4.add(num5))); 19 System.out.println("减:"+(num4.subtract(num5))); 20 System.out.println("乘:"+(num4.multiply(num5))); 21 System.out.println("除:"+(num4.divide(num5,5,BigDecimal.ROUND_HALF_DOWN))); 22 23 } 24 }
在有些项目中需要操作很大的数字的计算,在java中提供了两个常用的类来对大数字进行操作
时间: 2024-10-04 14:47:48