高精度操作数值 BigDecimal类和BinInteger类

Java1.1增加了两个类,用于进行高精度的计算,BigDecimal类和BinInteger类。尽管他们大致可以划分为"封装器"类型,但两者都没有对应的“主类型”。


原文地址:http://www.cnblogs.com/hellojava/archive/2013/01/06/2845423.html

1、简介

  JAVA中提供了可以进行大数字的操作类:BigDecimal和BinInteger,在java.math.*包下。其中BigDecimal是针对大小数的操作类,BinInteger是针对大整数的操作类,从它们的命名中也可以看出。BigDecimal的实现利用到了BigInteger,不同的是BigDecimal加入了小数的概念。一般的float型和Double型数据只可以用来做科学计算或者是工程计算,由于在商业计算中,要求的数字精度比较高,所以要用到java.math.BigDecimal类,它支持任何精度的定点数,可以用它来精确计算货币值。

2、BigDecimal类

  1、静态字段

static BigDecimal ONE 
          值为 1,标度为 0。 
static int ROUND_CEILING 
          接近正无穷大的舍入模式。 
static int ROUND_DOWN 
          接近零的舍入模式。 
static int ROUND_FLOOR 
          接近负无穷大的舍入模式。 
static int ROUND_HALF_DOWN 
          向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为上舍入的舍入模式。 
static int ROUND_HALF_EVEN 
          向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。 
static int ROUND_HALF_UP 
          向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向上舍入的舍入模式。 
static int ROUND_UNNECESSARY 
          断言请求的操作具有精确的结果,因此不需要舍入。 
static int ROUND_UP 
          舍入远离零的舍入模式。 
static BigDecimal TEN 
          值为 10,标度为 0。 
static BigDecimal ZERO 
          值为 0,标度为 0。


2、构造方法

BigDecimal(BigInteger val) 
          将 BigInteger 转换为 BigDecimal。 
BigDecimal(BigInteger unscaledVal, int scale) 
          将 BigInteger 非标度值和 int 标度转换为 BigDecimal。 
BigDecimal(BigInteger unscaledVal, int scale, MathContext mc) 
          将 BigInteger 非标度值和 int 标度转换为 BigDecimal(根据上下文设置进行舍入)。 
BigDecimal(BigInteger val, MathContext mc) 
          将 BigInteger 转换为 BigDecimal(根据上下文设置进行舍入)。 
BigDecimal(char[] in) 
          将 BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符序列。 
BigDecimal(char[] in, int offset, int len) 
          将 BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符序列,同时允许指定子数组。 
BigDecimal(char[] in, int offset, int len, MathContext mc) 
          将 BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符序列,同时允许指定子数组,并根据上下文设置进行舍入。 
BigDecimal(char[] in, MathContext mc) 
          将 BigDecimal 的字符数组表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符序列(根据上下文设置进行舍入)。 
BigDecimal(double val) 
          将 double 转换为 BigDecimal,后者是 double 的二进制浮点值准确的十进制表示形式。 
BigDecimal(double val, MathContext mc) 
          将 double 转换为 BigDecimal(根据上下文设置进行舍入)。 
BigDecimal(int val) 
          将 int 转换为 BigDecimal。 
BigDecimal(int val, MathContext mc) 
          将 int 转换为 BigDecimal(根据上下文设置进行舍入)。 
BigDecimal(long val) 
          将 long 转换为 BigDecimal。 
BigDecimal(long val, MathContext mc) 
          将 long 转换为 BigDecimal(根据上下文设置进行舍入)。 
BigDecimal(String val) 
          将 BigDecimal 的字符串表示形式转换为 BigDecimal。 
BigDecimal(String val, MathContext mc) 
          将 BigDecimal 的字符串表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符串(按照上下文设置进行舍入)。


3、方法

BigDecimal abs() 
          返回 BigDecimal,其值为此 BigDecimal 的绝对值,其标度为 this.scale()。 
 BigDecimal abs(MathContext mc) 
          返回其值为此 BigDecimal 绝对值的 BigDecimal(根据上下文设置进行舍入)。 
 BigDecimal add(BigDecimal augend) 
          返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。 
 BigDecimal add(BigDecimal augend, MathContext mc) 
          返回其值为 (this + augend) 的 BigDecimal(根据上下文设置进行舍入)。 
 byte byteValueExact() 
          将此 BigDecimal 转换为 byte,以检查丢失的信息。 
 int compareTo(BigDecimal val) 
          将此 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[] divideAndRemainder(BigDecimal divisor) 
          返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟对两个操作数计算所得到的 remainder。 
 BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) 
          返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟根据上下文设置对两个操作数进行舍入计算所得到的 remainder 的结果。 
 BigDecimal divideToIntegralValue(BigDecimal divisor) 
          返回 BigDecimal,其值为向下舍入所得商值 (this / divisor) 的整数部分。 
 BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc) 
          返回 BigDecimal,其值为 (this / divisor) 的整数部分。 
 double doubleValue() 
          将此 BigDecimal 转换为 double。 
 boolean equals(Object x) 
          比较此 BigDecimal 与指定的 Object 的相等性。 
 float floatValue() 
          将此 BigDecimal 转换为 float。 
 int hashCode() 
          返回此 BigDecimal 的哈希码。 
 int intValue() 
          将此 BigDecimal 转换为 int。 
 int intValueExact() 
          将此 BigDecimal 转换为 int,以检查丢失的信息。 
 long longValue() 
          将此 BigDecimal 转换为 long。 
 long longValueExact() 
          将此 BigDecimal 转换为 long,以检查丢失的信息。 
 BigDecimal max(BigDecimal val) 
          返回此 BigDecimal 和 val 的最大值。 
 BigDecimal min(BigDecimal val) 
          返回此 BigDecimal 和 val 的最小值。 
 BigDecimal movePointLeft(int n) 
          返回一个 BigDecimal,它等效于将该值的小数点向左移动 n 位。 
 BigDecimal movePointRight(int n) 
          返回一个 BigDecimal,它等效于将该值的小数点向右移动 n 位。 
 BigDecimal multiply(BigDecimal multiplicand) 
          返回一个 BigDecimal,其值为 (this × multiplicand),其标度为 (this.scale() + multiplicand.scale())。 
 BigDecimal multiply(BigDecimal multiplicand, MathContext mc) 
          返回其值为 (this × multiplicand) 的 BigDecimal(根据上下文设置进行舍入)。 
 BigDecimal negate() 
          返回 BigDecimal,其值为 (-this),其标度为 this.scale()。 
 BigDecimal negate(MathContext mc) 
          返回其值为 (-this) 的 BigDecimal(根据上下文设置进行舍入)。 
 BigDecimal plus() 
          返回 BigDecimal,其值为 (+this),其标度为 this.scale()。 
 BigDecimal plus(MathContext mc) 
          返回其值为 (+this) 的 BigDecimal(根据上下文设置进行舍入)。 
 BigDecimal pow(int n) 
          返回其值为 (thisn) 的 BigDecimal,准确计算该幂,使其具有无限精度。 
 BigDecimal pow(int n, MathContext mc) 
          返回其值为 (thisn) 的 BigDecimal。 
 int precision() 
          返回此 BigDecimal 的精度。 
 BigDecimal remainder(BigDecimal divisor) 
          返回其值为 (this % divisor) 的 BigDecimal。 
 BigDecimal remainder(BigDecimal divisor, MathContext mc) 
          返回其值为 (this % divisor) 的 BigDecimal(根据上下文设置进行舍入)。 
 BigDecimal round(MathContext mc) 
          返回根据 MathContext 设置进行舍入后的 BigDecimal。 
 int scale() 
          返回此 BigDecimal 的标度。 
 BigDecimal scaleByPowerOfTen(int n) 
          返回其数值等于 (this * 10n) 的 BigDecimal。 
 BigDecimal setScale(int newScale) 
          返回一个 BigDecimal,其标度为指定值,其值在数值上等于此 BigDecimal 的值。 
 BigDecimal setScale(int newScale, int roundingMode) 
          返回一个 BigDecimal,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。 
 BigDecimal setScale(int newScale, RoundingMode roundingMode) 
          返回 BigDecimal,其标度为指定值,其非标度值通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定,以维护其总值。 
 short shortValueExact() 
          将此 BigDecimal 转换为 short,以检查丢失的信息。 
 int signum() 
          返回此 BigDecimal 的正负号函数。 
 BigDecimal stripTrailingZeros() 
          返回数值上等于此小数,但从该表示形式移除所有尾部零的 BigDecimal。 
 BigDecimal subtract(BigDecimal subtrahend) 
          返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。 
 BigDecimal subtract(BigDecimal subtrahend, MathContext mc) 
          返回其值为 (this - subtrahend) 的 BigDecimal(根据上下文设置进行舍入)。 
 BigInteger toBigInteger() 
          将此 BigDecimal 转换为 BigInteger。 
 BigInteger toBigIntegerExact() 
          将此 BigDecimal 转换为 BigInteger,以检查丢失的信息。 
 String toEngineeringString() 
          返回此 BigDecimal 的字符串表示形式,需要指数时,则使用工程计数法。 
 String toPlainString() 
          返回不带指数字段的此 BigDecimal 的字符串表示形式。 
 String toString() 
          返回此 BigDecimal 的字符串表示形式,如果需要指数,则使用科学记数法。 
 BigDecimal ulp() 
          返回此 BigDecimal 的 ulp(最后一位的单位)的大小。 
 BigInteger unscaledValue() 
          返回其值为此 BigDecimal 的非标度值 的 BigInteger。 
static BigDecimal valueOf(double val) 
          使用 Double.toString(double) 方法提供的 double 规范的字符串表示形式将 double 转换为 BigDecimal。 
static BigDecimal valueOf(long val) 
          将 long 值转换为具有零标度的 BigDecimal。 
static BigDecimal valueOf(long unscaledVal, int scale) 
          将 long 非标度值和 int 标度转换为 BigDecimal。


4、实例

  转换

  将int、long、double、string类型的数值转为BigDecimal。使用double会造成精度丢失,而使用BigDecimal就是为了解决精度丢失的问题,建议使用String方式转换。

BigDecimal b2 = new BigDecimal(1.23456789);//会造成精度丢失
BigDecimal b1 = new BigDecimal("1.23456789");//不会造成精度丢失
b1.setScale(3, BigDecimal.ROUND_HALF_EVEN);//设置精度,在保留小数位时,要设置舍入模式

 反之,将BigDecimal转int、long、float、double类型。

BigDecimal b1 = new BigDecimal("1.23456789");
        
System.out.println(b1.doubleValue());
System.out.println(b1.floatValue());
System.out.println(b1.longValue());
System.out.println(b1.intValue());
System.out.println(b1.toString());

 运行结果:

1.23456789
1.2345679
1
1
1.23456789

  加减乘除

  可以进行正负数运算和显示。

BigDecimal big1 = new BigDecimal("600");
BigDecimal big2 = new BigDecimal("300");
// 两个BigDecimal相加
big1 = big1.add(big2);
// 两个BigDecimal相减,
big1 = big1.subtract(big2);
// 两个BigDecimal相乘
big1 = big1.multiply(big2);
// 两个BigDecimal相除。
big1 = big1.divide(big2);

 使用divide()方法注意:

  1. 相除的时候,被除数为0,会抛出异常:java.lang.ArithmeticException: Division by zero。
  2. 相除后为小数,则应该保留小数位,否则可能会抛出异常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result。
divide(BigDecimal divisor, int scale, int roundingMode);
b1.divide(b2, 2, BigDecimal.ROUND_CEILING);//采用ROUND_CEILING模式舍入,保留2位小数。

还有其他比较大小,设置和返回精度、标度等方法参考相关API。

3、BigInteger类

  BigInteger类用法跟BigDecimal类用法很相似,就不多啰嗦。

  静态字段

static BigInteger ONE 
          BigInteger 的常量 1。 
static BigInteger TEN 
          BigInteger 的常量 10。 
static BigInteger ZERO 
          BigInteger 的常量 0。 

构造方法
BigInteger(byte[] val) 
          将包含 BigInteger 的二进制补码表示形式的 byte 数组转换为 BigInteger。 
BigInteger(int signum, byte[] magnitude) 
          将 BigInteger 的符号-数量表示形式转换为 BigInteger。 
BigInteger(int bitLength, int certainty, Random rnd) 
          构造一个随机生成的正 BigInteger,它可能是一个具有指定 bitLength 的素数。 
BigInteger(int numBits, Random rnd) 
          构造一个随机生成的 BigInteger,它是在 0 到 (2numBits - 1)(包括)范围内均匀分布的值。 
BigInteger(String val) 
          将 BigInteger 的十进制字符串表示形式转换为 BigInteger。 
BigInteger(String val, int radix) 
          将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。 

方法
BigInteger abs() 
          返回其值是此 BigInteger 的绝对值的 BigInteger。 
 BigInteger add(BigInteger val) 
          返回其值为 (this + val) 的 BigInteger。 
 BigInteger and(BigInteger val) 
          返回其值为 (this & val) 的 BigInteger。 
 BigInteger andNot(BigInteger val) 
          返回其值为 (this & ~val) 的 BigInteger。 
 int bitCount() 
          返回此 BigInteger 的二进制补码表示形式中与符号不同的位的数量。 
 int bitLength() 
          返回此 BigInteger 的最小的二进制补码表示形式的位数,不包括 符号位。 
 BigInteger clearBit(int n) 
          返回其值与清除了指定位的此 BigInteger 等效的 BigInteger。 
 int compareTo(BigInteger val) 
          将此 BigInteger 与指定的 BigInteger 进行比较。 
 BigInteger divide(BigInteger val) 
          返回其值为 (this / val) 的 BigInteger。 
 BigInteger[] divideAndRemainder(BigInteger val) 
          返回包含 (this / val) 后跟 (this % val) 的两个 BigInteger 的数组。 
 double doubleValue() 
          将此 BigInteger 转换为 double。 
 boolean equals(Object x) 
          比较此 BigInteger 与指定的 Object 的相等性。 
 BigInteger flipBit(int n) 
          返回其值与对此 BigInteger 进行指定位翻转后的值等效的 BigInteger。 
 float floatValue() 
          将此 BigInteger 转换为 float。 
 BigInteger gcd(BigInteger val) 
          返回一个 BigInteger,其值是 abs(this) 和 abs(val) 的最大公约数。 
 int getLowestSetBit() 
          返回此 BigInteger 最右端(最低位)1 比特的索引(即从此字节的右端开始到本字节中最右端 1 比特之间的 0 比特的位数)。 
 int hashCode() 
          返回此 BigInteger 的哈希码。 
 int intValue() 
          将此 BigInteger 转换为 int。 
 boolean isProbablePrime(int certainty) 
          如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false。 
 long longValue() 
          将此 BigInteger 转换为 long。 
 BigInteger max(BigInteger val) 
          返回此 BigInteger 和 val 的最大值。 
 BigInteger min(BigInteger val) 
          返回此 BigInteger 和 val 的最小值。 
 BigInteger mod(BigInteger m) 
          返回其值为 (this mod m) 的 BigInteger。 
 BigInteger modInverse(BigInteger m) 
          返回其值为 (this-1 mod m) 的 BigInteger。 
 BigInteger modPow(BigInteger exponent, BigInteger m) 
          返回其值为 (thisexponent mod m) 的 BigInteger。 
 BigInteger multiply(BigInteger val) 
          返回其值为 (this * val) 的 BigInteger。 
 BigInteger negate() 
          返回其值是 (-this) 的 BigInteger。 
 BigInteger nextProbablePrime() 
          返回大于此 BigInteger 的可能为素数的第一个整数。 
 BigInteger not() 
          返回其值为 (~this) 的 BigInteger。 
 BigInteger or(BigInteger val) 
          返回其值为 (this | val) 的 BigInteger。 
 BigInteger pow(int exponent) 
          返回其值为 (thisexponent) 的 BigInteger。 
static BigInteger probablePrime(int bitLength, Random rnd) 
          返回有可能是素数的、具有指定长度的正 BigInteger。 
 BigInteger remainder(BigInteger val) 
          返回其值为 (this % val) 的 BigInteger。 
 BigInteger setBit(int n) 
          返回其值与设置了指定位的此 BigInteger 等效的 BigInteger。 
 BigInteger shiftLeft(int n) 
          返回其值为 (this << n) 的 BigInteger。 
 BigInteger shiftRight(int n) 
          返回其值为 (this >> n) 的 BigInteger。 
 int signum() 
          返回此 BigInteger 的正负号函数。 
 BigInteger subtract(BigInteger val) 
          返回其值为 (this - val) 的 BigInteger。 
 boolean testBit(int n) 
          当且仅当设置了指定的位时,返回 true。 
 byte[] toByteArray() 
          返回一个 byte 数组,该数组包含此 BigInteger 的二进制补码表示形式。 
 String toString() 
          返回此 BigInteger 的十进制字符串表示形式。 
 String toString(int radix) 
          返回此 BigInteger 的给定基数的字符串表示形式。 
static BigInteger valueOf(long val) 
          返回其值等于指定 long 的值的 BigInteger。 
 BigInteger xor(BigInteger val) 
          返回其值为 (this ^ val) 的 BigInteger。 



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值