栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【java笔记】大数操作(BigInteger)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【java笔记】大数操作(BigInteger)

如果操作的时候,一个整数数据已经超过了整数的最大类型长度long的话,则此数据就无法装入,所以,此时要使用BigInteger类进行操作

导包:import java.math

方法列表:
描述
public BigInteger(String val)将一个字符串变为BigInteger类型
public BigInteger add(BigInterger val)加法
public Biginteger subtract(BigInterger val)减法
public Biginteger multiply(BigInteger val)乘法
public Biginteger divide(BigInteger val)除法
public Biginteger max(BigInteger val)返回两个大数字的最大值
public Biginteger min(BigInteger val)返回两个大数字的最小值
public Biginteger[] divideAndRemainder(BigInteger val)除法操作,数组的第一个元素作为除法的商,第二个元素作为除法的余数

测试:

public class Demo {
    public static void main(String[] args) {
    String s1="123456781230123";
    String s2="987654321234567";
    BigInteger num1=new BigInteger(s1);
    BigInteger num2=new BigInteger(s2);
        System.out.println(num1.add(num2));//1111111102464690
        System.out.println(num1.subtract(num2));//-864197540004444
        System.out.println(num1.multiply(num2));//121932623467641563114289261741
        System.out.println(num2.divideAndRemainder(num1)[0]);//8
        System.out.println(num2.divideAndRemainder(num1)[1]);//71393583

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769765.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号