第三次学JAVA再学不好就吃翔(part70)--BigInteger类

学习笔记,仅供参考,有错必纠



BigInteger类


BigInteger类是在java.math包下的一个类,使用该类时需要导包。该类可以让超过Integer范围的数据进行运算。


构造方法


public BigInteger(String val)

将 BigInteger 的十进制字符串表示形式转换为 BigInteger。


方法


add方法


public BigInteger add(BigInteger val)
  • 参数

    • val - 将添加到此 BigInteger 中的值。
  • 返回

    • this + val

subtract方法


public BigInteger subtract(BigInteger val)
  • 参数

    • val - 从此 BigInteger 中减去的值。
  • 返回

    • this - val

multiply方法


public BigInteger multiply(BigInteger val)
  • 参数

    • val - 要乘以此 BigInteger 的值。
  • 返回

    • this * val

divide方法


public BigInteger divide(BigInteger val)
  • 参数

    • val - 此 BigInteger 要除以的值。
  • 返回

    • this / val

divideAndRemainder方法


public BigInteger[] divideAndRemainder(BigInteger val)

返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。

  • 参数

    • val - 此 BigInteger 要除以的值和计算所得的余数。
  • 返回

    • 两个 BigInteger 的数组:商 (this / val) 是初始元素,余数 (this % val) 是最终元素。

举个例子


package com.guiyang.restudy3;

import java.math.BigInteger;

public class D4BigInteger {

	public static void main(String[] args) {
		BigInteger bi1 = new BigInteger("100");
		BigInteger bi2 = new BigInteger("2");
		
		System.out.println(bi1.add(bi2)); 				//+
		System.out.println(bi1.subtract(bi2));			//-
		System.out.println(bi1.multiply(bi2)); 			//*
		System.out.println(bi1.divide(bi2));    		///(除)
		
		System.out.println("------");
		
		BigInteger[] arr = bi1.divideAndRemainder(bi2);	//取除数和余数
		
		for (int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);
		}
	}

}

输出:

102
98
200
50
------
50
0

猜你喜欢

转载自blog.csdn.net/m0_37422217/article/details/106798802