8.6 BigInteger类

本文介绍了Java中BigInteger类的基本用法及其实现任意精度整数运算的方法,包括加减乘除等操作,并通过示例展示了如何使用BigInteger类进行大整数运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8.6.2 BigInteger类

BigInteger类提供任意精度的整数运算

常用方法:

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 compareTo(BigInteger val) //返回当前大整数对象与参数指定的大整数的比较结果,返回值是1、-1或者0,分别表示当前大整数对象大于、小于或者等于参数指定的大整数
public BigInteger abs() //返回当前大整数对象的绝对值
public BigInteger pow(int a) //返回当前大整数对象的a次幂
public BigInteger toString() //返回当前大整数对象十进制的字符串表示
public BigInteger toString(int p)  //返回当前大整数对象p进制的字符串表示

/**
 * 计算5的平方根以及两个大整数的和与积
 * @author night
 *
 */
public class Example8_18 {

	public static void main(String[] args) {
		
		double a =5.0;
		double st=Math.sqrt(a);
		System.out.println(a+"的平方根:"+st);
		
		BigInteger bigInteger=new BigInteger("0"),
		 one=new BigInteger("123456789"),
		two=new BigInteger("987654321");
		bigInteger=one.add(two);
		System.out.println("和:"+bigInteger);
		bigInteger=one.multiply(two);
		System.out.println("积:"+bigInteger);
		
	}

}
/*
5.0的平方根:2.23606797749979
和:1111111110
积:121932631112635269
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值