JAVA大数处理(BigInteger,BigDecimal)

 JAVA大数处理(BigInteger,BigDecimal)

     在用C或者C++处理大数时感觉非常麻烦,但是在JAVA中有两个类BigIntegerBigDecimal分别表示大整数类和大浮点数类,至于两个类的对象能表示最大范围不清楚,理论上能够表示无线大的数,只要计算机内存足够大。

这两个类都在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());

}

.运用

四则预算:

import java.util.Scanner;
import java.math.*;
import java.text.*;

public class Main
{
public static void main(String args[])
{
   Scanner cin = new Scanner ( System.in );
   BigInteger a,b;
   int c;
   char op;
   String s;
  
   while( cin.hasNext() )
   {
    a = cin.nextBigInteger();
    s = cin.next();
    op = s.charAt(0);
    if( op == '+')
    {
     b = cin.nextBigInteger();
     System.out.println(a.add(b));
    }
    else if( op == '-')
    {
     b = cin.nextBigInteger();
     System.out.println(a.subtract(b));
    }
    else if( op == '*')
    {
     b = cin.nextBigInteger();
     System.out.println(a.multiply(b));
    }
    else
    {
     BigDecimal a1,b1,eps;
     String s1,s2,temp;
     s1 = a.toString();
       a1 = new BigDecimal(s1);
     b = cin.nextBigInteger();
     s2 = b.toString();
     b1 = new BigDecimal(s2);
     c = cin.nextInt();
     eps = a1.divide(b1,c,4);
     //System.out.println(a + " " + b + " " + c);
     //System.out.println(a1.doubleValue() + " " + b1.doubleValue() + " " + c);
     System.out.print( a.divide(b) + " " + a.mod(b) + " ");
     if( c != 0)
     {
      temp = "0.";
      for(int i = 0; i < c; i ++) temp += "0";
      DecimalFormat gd = new DecimalFormat(temp);
      System.out.println(gd.format(eps));
     }
     else System.out.println(eps);
    }
   }
   }
}

补充:

        a=a.pow(b);
        a=a.stripTrailingZeros();
         d=a.toPlainString();
        
if(d.charAt(0)=='0') d=d.substring(1);

  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: BigInteger转换为BigDecimal可以通过BigDecimal的构造函数来实现,将BigInteger作为参数传递给BigDecimal构造函数即可。 例如: BigInteger bigInteger = new BigInteger("12345678901234567890"); BigDecimal bigDecimal = new BigDecimal(bigInteger); 这样就将BigInteger类型的bigInteger转换为了BigDecimal类型的bigDecimal。 ### 回答2: BigIntegerBigDecimal都是Java处理大数的类。BigInteger表示不可变的任意精度整数,而BigDecimal表示不可变的任意精度十进制数。 要将BigInteger转换为BigDecimal,可以使用BigDecimal的构造方法来完成转换。BigDecimal的构造方法接受一个BigInteger类型的参数,可以直接将BigInteger转换为相应的BigDecimal对象。 以下是将BigInteger对象转换为BigDecimal对象的示例代码: ```java BigInteger bigInteger = new BigInteger("1234567890"); BigDecimal bigDecimal = new BigDecimal(bigInteger); ``` 上述代码中,首先创建了一个BigInteger对象bigInteger,其值为"1234567890"。然后使用BigDecimal的构造方法将bigInteger转换为相应的BigDecimal对象bigDecimal。 通过以上代码,我们成功将BigInteger对象转换为BigDecimal对象。这样就可以继续使用BigDecimal提供的各种方法进行高精度的数值计算。 需要注意的是,由于BigDecimal表示的是十进制数,而BigInteger表示的是整数,所以在转换时可能会导致精度的损失或受到舍入模式的影响。因此,在进行大数计算时,需要根据具体的需求选择合适的转换方式和精度控制。 ### 回答3: 在Java中,BigIntegerBigDecimal是数字处理中非常常用的两个类。BigInteger用于处理任意长度的整数,而BigDecimal用于处理任意精度的小数。 要将BigInteger转换为BigDecimal,可以使用BigDecimal的构造函数。BigDecimal类有一个可以接受BigInteger值的构造函数,这样可以将BigInteger的值转换为BigDecimal。 以下是一个简单的示例代码,演示如何将BigInteger转换为BigDecimal: ```java import java.math.BigDecimal; import java.math.BigInteger; public class BigIntegerToBigDecimal { public static void main(String[] args) { BigInteger bigInteger = new BigInteger("1234567890"); BigDecimal bigDecimal = new BigDecimal(bigInteger); System.out.println("BigInteger: " + bigInteger); System.out.println("BigDecimal: " + bigDecimal); } } ``` 在上述示例中,我们首先创建了一个BigInteger对象(bigInteger),并将其初始化为1234567890。然后,我们使用BigDecimal的构造函数将bigInteger转换为BigDecimal(bigDecimal)。最后,我们将bigIntegerbigDecimal的值输出到控制台上。 输出结果: BigInteger: 1234567890 BigDecimal: 1234567890 通过这种方式,我们可以将BigInteger转换为BigDecimal,用于处理高精度的小数计算。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值