java里的BigInteger和BigDecimal

阶乘:

View Code
 1 import java.io.*;
 2 import java.math.*;
 3 public class BigInteger_factorial {
 4   public static void main(String[] args) throws IOException{
 5          BigInteger s = BigInteger.valueOf(1);
 6          for ( int i = 1;i<=500 ; i++){
 7               s = s.multiply(BigInteger.valueOf(i));
 8               System.out.println(s);
 9         }
10   }
11 }

大数计算hrbustoj1582:

View Code
 1 import java.math.*;
 2 import java.util.*;
 3 import java.io.*;
 4 
 5 public class Main {
 6     Scanner cin = new Scanner(new BufferedInputStream(System.in));
 7     public void solve() {
 8         String s1, s2;
 9         s1 = cin.next();
10         s2 = cin.next();
11         if (s1.charAt(0) == '+') s1 = s1.substring(1, s1.length());
12         if (s2.charAt(0) == '+') s2 = s2.substring(1, s2.length());
13         BigInteger a = new BigInteger(s1), b = new BigInteger(s2);
14         System.out.println(a.add(b));
15     }
16     public static void main(String args[]) {
17         Main test = new Main();
18         while (test.cin.hasNext()) {
19             test.solve();
20         }
21     }
22 }

BigInteger和BigDecimal可以说是acmer选择java的首要原因。 函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是

BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().

BigInteger

主要API

将字符串转换成BigInteger

BigInteger(String val)                 将 BigInteger 的十进制字符串表示形式转换为 BigInteger。

BigInteger(String val, int radix)            将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。

BigInteger的加法

BigInteger

add(BigInteger val)      返回其值为 (this + val) 的 BigInteger。

BigInteger

and(BigInteger val)      返回其值为 (this & val) 的 BigInteger。

BigInteger的减法

BigInteger

subtract(BigInteger val)      返回其值为 (this - val) 的 BigInteger。

BigInteger的乘法

BigInteger

multiply(BigInteger val)      返回其值为 (this * val) 的 BigInteger。

大数求余:

BigInteger

mod(BigInteger m)       返回其值为 (this mod m) 的 BigInteger。

 大数除法

BigInteger

divide(BigInteger val)     返回其值为 (this / val) 的 BigInteger。

其他一些

BigInteger

gcd(BigInteger val)                          返回一个 BigInteger,其值是 abs(this) 和 abs(val) 的最大公约数。

 

BigInteger

max(BigInteger val)             返回此 BigInteger 和 val 的最大值。

BigInteger

min(BigInteger val)              返回此 BigInteger 和 val 的最小值。

BigDecimal类

主要API:

将字符串转换成BigDecimal

BigDecimal(String val)                                              将 BigDecimal 的字符串表示形式转换为 BigDecimal。

BigDecimal(String val,MathContext mc)                     将 BigDecimal 的字符串表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法

                                                                                相同的字符串(按照上下文设置进行舍入)。

两个BigDecimal的相加

BigDecimal

add(BigDecimal augend)                        返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。

BigDecimal

add(BigDecimal augend,MathContext mc)返回其值为 (this + augend) 的 BigDecimal(根据上下文设置进行舍入)。

两个BigDecimal的相减

BigDecimal

subtract(BigDecimal subtrahend)              返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。

BigDecimal

subtract(BigDecimal subtrahend,MathContext mc)返回其值为 (this - subtrahend) 的 BigDecimal(根据上下文设置进行舍入)。

两个BigDecimal的相除:

BigDecimal

divide(BigDecimal divisor)                       返回一个 BigDecimal,其值为 (this / divisor),其首选标度为 (this.scale() - divisor.scale());

                                                              如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出 ArithmeticException。

BigDecimal

divide(BigDecimal divisor, int roundingMode)        返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。

BigDecimal

divide(BigDecimal divisor, int scale, int roundingMode)   返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。

BigDecimal

divide(BigDecimal divisor, int scale,RoundingMode roundingMode) 返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。

BigDecimal

divide(BigDecimal divisor,MathContext mc)                返回其值为 (this / divisor) 的 BigDecimal(根据上下文设置进行舍入)。

BigDecimal

divide(BigDecimal divisor,RoundingMode roundingMode)      返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。

计算BigDecimal的N次幂

BigDecimal

pow(int n)                                返回其值为 (thisn) 的 BigDecimal,准确计算该幂,使其具有无限精度。

BigDecimal

pow(int n, MathContext mc)                       返回其值为 (thisn) 的 BigDecimal。

有关转换成字符串的方法

String

toEngineeringString()                              返回此 BigDecimal 的字符串表示形式,需要指数时,则使用工程计数法。

String

toPlainString()                                  返回不带指数字段的此 BigDecimal 的字符串表示形式。

String

toString()                                    返回此 BigDecimal 的字符串表示形式,如果需要指数,则使用科学记数法。

 

posted @ 2013-01-11 15:52  _sunshine  阅读(428)  评论(0编辑  收藏  举报