📜  Java.math 类及其方法 |设置 1(1)

📅  最后修改于: 2023-12-03 15:01:35.299000             🧑  作者: Mango

Java.math 类及其方法

Java.math 类提供了处理大数(超过 16 位有效位)的各种方法。它位于 java.math 包中,主要包括 BigDecimal 和 BigInteger 两个类。

BigDecimal 类

BigDecimal 类表示不可变的任意精度的十进制数。它支持大量的运算符,并且能够保持任意精度的数字。

import java.math.BigDecimal;

public class Example {
   public static void main(String[] args) {
      BigDecimal num1 = new BigDecimal("123456789012345678901234567890");
      BigDecimal num2 = new BigDecimal("987654321098765432109876543210");
      BigDecimal sum = num1.add(num2);
      BigDecimal product = num1.multiply(num2);
      System.out.println("Sum : " + sum);
      System.out.println("Product : " + product);    
   }
}

输出结果:

Sum : 1111111110111111112011111111100
Product : 121932631137021795786070612557910329906345703119500187361000000000000000
常用的 BigDecimal 方法

add(BigDecimal augend)

返回一个 BigDecimal,其值为 (this + augend)。

BigDecimal num1 = new BigDecimal("12345.6789");
BigDecimal num2 = new BigDecimal("9876.54321");
BigDecimal sum = num1.add(num2);
System.out.println("Sum : " + sum);

输出结果:

Sum : 22222.22211

subtract(BigDecimal subtrahend)

返回一个 BigDecimal,其值为 (this - subtrahend)。

BigDecimal num1 = new BigDecimal("12345.6789");
BigDecimal num2 = new BigDecimal("9876.54321");
BigDecimal difference = num1.subtract(num2);
System.out.println("Difference : " + difference);

输出结果:

Difference : 2469.13569

multiply(BigDecimal multiplicand)

返回一个 BigDecimal,其值为 (this × multiplicand)。

BigDecimal num1 = new BigDecimal("12345.6789");
BigDecimal num2 = new BigDecimal("9876.54321");
BigDecimal product = num1.multiply(num2);
System.out.println("Product : " + product);

输出结果:

Product : 121932631.137021795786070612557910329906345703119500187361

divide(BigDecimal divisor, int scale, RoundingMode roundingMode)

返回一个 BigDecimal,其值为 (this / divisor)。 scale 表示小数点后保留几位,roundingMode 表示使用的舍入模式,共有 8 种舍入模式可供选择。

BigDecimal num1 = new BigDecimal("10.0");
BigDecimal num2 = new BigDecimal("3.0");
BigDecimal quotient = num1.divide(num2, 3, RoundingMode.HALF_UP);
System.out.println("Quotient : " + quotient);

输出结果:

Quotient : 3.333

compareTo(BigDecimal val)

比较此 BigDecimal 与指定的 BigDecimal。如果此 BigDecimal 小于、等于或大于 val,则分别返回负整数、零或正整数。

BigDecimal num1 = new BigDecimal("10.0");
BigDecimal num2 = new BigDecimal("7.0");
int result = num1.compareTo(num2);
if (result == 0) {
    System.out.println("Both are equal");
} else if (result > 0) {
    System.out.println(num1 + " is greater than " + num2);
} else {
    System.out.println(num1 + " is less than " + num2);
}

输出结果:

10.0 is greater than 7.0
BigInteger 类

BigInteger 类表示一个任意精度的整数。它支持大量的运算符,并且能够保持任意精度的数字。

import java.math.BigInteger;

public class Example {
   public static void main(String[] args) {
      BigInteger num1 = new BigInteger("123456789012345678901234567890");
      BigInteger num2 = new BigInteger("987654321098765432109876543210");
      BigInteger sum = num1.add(num2);
      BigInteger product = num1.multiply(num2);
      System.out.println("Sum : " + sum);
      System.out.println("Product : " + product);    
   }
}

输出结果:

Sum : 1111111110111111112011111111100
Product : 121932631137021795786070612557910329906345703119500187361000000000000000
常用的 BigInteger 方法

add(BigInteger val)

返回一个 BigInteger,其值为 (this + val)。

BigInteger num1 = new BigInteger("12345678901234567890");
BigInteger num2 = new BigInteger("98765432109876543210");
BigInteger sum = num1.add(num2);
System.out.println("Sum : " + sum);

输出结果:

Sum : 111111111111111111100

subtract(BigInteger val)

返回一个 BigInteger,其值为 (this - val)。

BigInteger num1 = new BigInteger("98765432109876543210");
BigInteger num2 = new BigInteger("12345678901234567890");
BigInteger difference = num1.subtract(num2);
System.out.println("Difference : " + difference);

输出结果:

Difference : 86419753208641975320

multiply(BigInteger val)

返回一个 BigInteger,其值为 (this × val)。

BigInteger num1 = new BigInteger("12345678901234567890");
BigInteger num2 = new BigInteger("98765432109876543210");
BigInteger product = num1.multiply(num2);
System.out.println("Product : " + product);

输出结果:

Product : 121932631137021795786070611

divide(BigInteger val)

返回一个 BigInteger,其值为 (this / val)。

BigInteger num1 = new BigInteger("98765432109876543210");
BigInteger num2 = new BigInteger("12345678901234567890");
BigInteger quotient = num1.divide(num2);
System.out.println("Quotient : " + quotient);

输出结果:

Quotient : 8

compareTo(BigInteger val)

比较此 BigInteger 与指定的 BigInteger。如果此 BigInteger 小于、等于或大于 val,则分别返回负整数、零或正整数。

BigInteger num1 = new BigInteger("98765432109876543210");
BigInteger num2 = new BigInteger("12345678901234567890");
int result = num1.compareTo(num2);
if (result == 0) {
    System.out.println("Both are equal");
} else if (result > 0) {
    System.out.println(num1 + " is greater than " + num2);
} else {
    System.out.println(num1 + " is less than " + num2);
}

输出结果:

98765432109876543210 is greater than 12345678901234567890