BigInteger和BigDecimal

在用C或者C++处理大数时感觉非常麻烦,但是在Java中有两个类

BigInteger和BigDecimal

分别表示大整数类和大浮点数类,至于两个类的对象能表示最大范围不清楚,理论上能够表示无线大的数,只要计算机内存足够大。
这两个类都在java.math.*包中,因此每次必须在开头处引用该包。

Ⅰ基本函数:
1.valueOf(parament); 将参数转换为制定的类型
比如 int a=3;
BigInteger b=BigInteger.valueOf(a);
则b=3;
String s=”12345”;
BigInteger c=BigInteger.valueOf(s);
则c=12345;

2.add(); 大整数相加
BigInteger a=new BigInteger(“23”);
BigInteger b=new BigInteger(“34”);
a. add(b);

3.subtract(); 相减
4.multiply(); 相乘
5.divide(); 相除取整
6.remainder(); 取余
7.pow(); a.pow(b)=a^b
8.gcd(); 最大公约数
9.abs(); 绝对值
10.negate(); 取反数
11.mod(); a.mod(b)=a%b=a.remainder(b);
12.max(); min();
13.punlic int comareTo();
14.boolean equals(); 是否相等
15.BigInteger构造函数:
一般用到以下两种:
BigInteger(String val);
将指定字符串转换为十进制表示形式;
BigInteger(String val,int radix);
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger
Ⅱ.基本常量:
A=BigInteger.ONE 1
B=BigInteger.TEN 10
C=BigInteger.ZERO 0
Ⅲ.基本操作

  1. 读入:
    用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中
    Scanner cin=new Scanner(System.in);// 读入
    while(cin.hasNext()) //等同于!=EOF
    {
    int n;
    BigInteger m;
    n=cin.nextInt(); //读入一个int;
    m=cin.BigInteger();//读入一个BigInteger;
    System.out.print(m.toString());
    }

if( a.compareTo(b) == 0 ) System.out.println(“a == b”); //大整数a==b
else if( a.compareTo(b) > 0 ) System.out.println(“a > b”); //大整数a>b
else if( a.compareTo(b) < 0 ) System.out.println(“a < b”); //大整数a<b

//大整数绝对值
System.out.println(a.abs()); //大整数a的绝对值

//大整数的幂
int exponent=10;
System.out.println(a.pow(exponent)); //大整数a的exponent次幂

//返回大整数十进制的字符串表示
System.out.println(a.toString());

//返回大整数p进制的字符串表示
int p=8;
System.out.println(a.toString§);

转换进制

String string1 = new BigInteger("20", 10).toString(2);
        Log.d("TAG","十进制的20转换成二进制是:"+string1);
 
        String string2 = new BigInteger("20", 10).toString(8);
        Log.d("TAG","十进制的20转换成八进制是:"+string2);
 
        String string3 = new BigInteger("20", 10).toString(16);
        Log.d("TAG","十进制的20转换成十六进制是:"+string3);
 
        String string4 = new BigInteger("110", 2).toString(10);
        Log.d("TAG","二进制的110转换成十进制是:"+string4);
 
        String string5 = new BigInteger("110", 8).toString(10);
        Log.d("TAG","八进制的110转换成十进制是:"+string5);
 
        String string6 = new BigInteger("110", 16).toString(10);
        Log.d("TAG","十六进制的110转换成十进制是:"+string6);

主要方法总结:

BigInteger abs()  返回大整数的绝对值
BigInteger add(BigInteger val) 返回两个大整数的和
BigInteger and(BigInteger val)  返回两个大整数的按位与的结果
BigInteger andNot(BigInteger val) 返回两个大整数与非的结果
BigInteger divide(BigInteger val)  返回两个大整数的商
double doubleValue()   返回大整数的double类型的值
float floatValue()   返回大整数的float类型的值
BigInteger gcd(BigInteger val)  返回大整数的最大公约数
int intValue() 返回大整数的整型值
long longValue() 返回大整数的long型值
BigInteger max(BigInteger val) 返回两个大整数的最大者
BigInteger min(BigInteger val) 返回两个大整数的最小者
BigInteger mod(BigInteger val) 用当前大整数对val求模
BigInteger multiply(BigInteger val) 返回两个大整数的积
BigInteger negate() 返回当前大整数的相反数
BigInteger not() 返回当前大整数的非
BigInteger or(BigInteger val) 返回两个大整数的按位或
BigInteger pow(int exponent) 返回当前大整数的exponent次方
BigInteger remainder(BigInteger val) 返回当前大整数除以val的余数
BigInteger leftShift(int n) 将当前大整数左移n位后返回
BigInteger rightShift(int n) 将当前大整数右移n位后返回
BigInteger subtract(BigInteger val)返回两个大整数相减的结果
byte[] toByteArray(BigInteger val)将大整数转换成二进制反码保存在byte数组中
String toString() 将当前大整数转换成十进制的字符串形式
BigInteger xor(BigInteger val) 返回两个大整数的异或
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值