您的位置:首页 > 编程语言 > Java开发

java里的BigInteger和BigDecimal

2013-01-11 15:52 686 查看
阶乘:

View Code

import java.math.*;
import java.util.*;
import java.io.*;

public class Main {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
public void solve() {
String s1, s2;
s1 = cin.next();
s2 = cin.next();
if (s1.charAt(0) == '+') s1 = s1.substring(1, s1.length());
if (s2.charAt(0) == '+') s2 = s2.substring(1, s2.length());
BigInteger a = new BigInteger(s1), b = new BigInteger(s2);
System.out.println(a.add(b));
}
public static void main(String args[]) {
Main test = new Main();
while (test.cin.hasNext()) {
test.solve();
}
}
}


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 的字符串表示形式,如果需要指数,则使用科学记数法。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: