Java BigInteger詳解


BigInteger概述

可用於無限大的整數計算

所在的包

java.math.BigInteger;

構造函數

public BigInteger(String val)

成員函數

 

 比較大小函數

public int compareTo(BigInteger val)

實例

a.compareTo(b)

如果a>b,返回值大於零

a<b,返回值小於零

a=b,返回值等於零

 

加法函數

public BigInteger add(BigInteger val)

減法函數

public BigInteger subtract(BigInteger val)

乘法函數

public BigInteger multiply(BigInteger val)

除法函數

public BigInteger divide(BigInteger val)

取余函數

public BigInteger remainder(BigInteger val)

取除數和余數函數

public BigInteger[] divideAndRemainder(BigInteger val)

 實例

import java.math.*;
public class Main {
    public static void main(String[] args)
    {
        BigInteger bi1 = new BigInteger("20");
        BigInteger bi2 = new BigInteger("5");
        //加法
        System.out.println("20+5結果:"+bi1.add(bi2));
        //減法
        System.out.println("20-5結果:"+bi1.subtract(bi2));
        //乘法
        System.out.println("20×5結果:"+bi1.multiply(bi2));
        //除法
        System.out.println("20÷5結果:"+bi1.divide(bi2));
        //取余
        System.out.println("20%5結果:"+bi1.remainder(bi2));
        //取除數和余數
        BigInteger[] bigIntegers = bi1.divideAndRemainder(bi2);
        for(BigInteger bi :bigIntegers)
        {
        System.out.println(bi);
        }
    }
}
/*
輸出
20+5結果:25
20-5結果:15
20×5結果:100
20÷5結果:4
20%5結果:0
4
0
*/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM