参考 http://lavasoft.blog.51cto.com/62575/228705/
从Java4到Java5,Java对BigInteger、BigDecimal两个类功能一直再做扩展与改进。主要原因是这两个数据类型很重要,在高精度的计算中全靠这两个数据类型了。BigInteger和BigDecimal分别表示任意精度的整数与浮点数。
本文中不在追溯各个版本的变化,只看Java5中两个类的使用。
一、java.math.BigInteger
不可变的任意精度的整数。 此类的用法比较简单些,也不存在舍入等操作。
1 2 package lavasoft; 3 4 import java.math.BigInteger; 5 import java.util.Random; 6 7 /** 8 * 测试BigInteger 9 * 10 * @author leizhimin 2009-11-17 12:49:41 11 */ 12 public class TestBigInteger { 13 public static void main(String[] args) { 14 System.out.println("-------------------构造BigInteger---------------------"); 15 //通过byte数组来创建BigInteger 16 BigInteger bi1 = new BigInteger(new byte[]{1, 1}); 17 System.out.println("bi1=" + bi1.toString()); 18 //创建带符号的BigInteger 19 BigInteger bi2 = new BigInteger(-1, new byte[]{1, 1}); 20 System.out.println("bi2=" + bi2.toString()); 21 //创建带符号的BigInteger随机数 22 BigInteger bi3 = new BigInteger(128, 20, new Random()); 23 System.out.println("bi3=" + bi3.toString()); 24 //通过10进制字符串创建带符号的BigInteger 25 BigInteger bi4 = new BigInteger("12342342342342123423423412341"); 26 System.out.println("bi4=" + bi4.toString()); 27 //通过10进制字符串创建带符号的BigInteger 28 BigInteger bi5 = new BigInteger("88888888888888888888888888888", Character.digit('a', 33)); 29 System.out.println("bi5=" + bi5.toString()); 30 System.out.println("BigInteger的常量:"); 31 System.out.println("BigInteger.ZERO=" + BigInteger.ZERO); 32 System.out.println("BigInteger.ONE=" + BigInteger.ONE); 33 System.out.println("BigInteger.TEN=" + BigInteger.TEN); 34 35 System.out.println("-------------------使用BigInteger---------------------"); 36 System.out.println("bi1的相反数=" + bi1.negate()); 37 System.out.println("bi1的相反数=" + bi1.negate()); 38 System.out.println("bi1+bi2=" + bi1.add(bi2)); 39 System.out.println("bi1-bi2=" + bi1.subtract(bi2)); 40 System.out.println("bi1*bi2=" + bi1.multiply(bi2)); 41 System.out.println("bi1/bi2=" + bi1.divide(bi2)); 42 System.out.println("bi1的10次方=" + bi1.pow(10)); 43 System.out.println("bi1的10次方=" + bi1.pow(1)); 44 BigInteger[] bx = bi4.divideAndRemainder(bi1); 45 System.out.println(">>>:bx[0]=" + bx[0] + ",bx[1]=" + bx[1]); 46 System.out.println("bi2的绝对值=" + bi2.abs()); 47 } 48 } 49
运行结果:
-------------------构造BigInteger---------------------
bi1=257
bi2=-257
bi3=175952079487573456985958549621373190227
bi4=12342342342342123423423412341
bi5=88888888888888888888888888888
BigInteger的常量:
BigInteger.ZERO=0
BigInteger.ONE=1
BigInteger.TEN=10
-------------------使用BigInteger---------------------
bi1的相反数=-257
bi1的相反数=-257
bi1+bi2=0
bi1-bi2=514
bi1*bi2=-66049
bi1/bi2=-1
bi1的10次方=1256988294225653106805249
bi1的10次方=257
>>>:bx[0]=48024678374872075577523005,bx[1]=56
bi2的绝对值=257
Process finished with exit code 0
bi1=257
bi2=-257
bi3=175952079487573456985958549621373190227
bi4=12342342342342123423423412341
bi5=88888888888888888888888888888
BigInteger的常量:
BigInteger.ZERO=0
BigInteger.ONE=1
BigInteger.TEN=10
-------------------使用BigInteger---------------------
bi1的相反数=-257
bi1的相反数=-257
bi1+bi2=0
bi1-bi2=514
bi1*bi2=-66049
bi1/bi2=-1
bi1的10次方=1256988294225653106805249
bi1的10次方=257
>>>:bx[0]=48024678374872075577523005,bx[1]=56
bi2的绝对值=257
Process finished with exit code 0
二、java.math.BigDecimal
不可变的、任意精度的有符号十进制数。与之相关的还有两个类:
java.math.MathContext:
该对象是封装上下文设置的不可变对象,它描述数字运算符的某些规则,如数据的精度,舍入方式等。
java.math.RoundingMode:这是一种枚举类型,定义了很多常用的数据舍入方式。
这个类用起来还是很比较复杂的,原因在于舍入模式,数据运算规则太多太多,不是数学专业出身的人看着中文API都难以理解,这些规则在实际中使用的时候在翻阅都来得及。
1 package lavasoft; 2 3 import java.math.BigDecimal; 4 import java.math.MathContext; 5 import java.math.RoundingMode; 6 7 /** 8 * 测试BigDecimal 9 * 10 * @author leizhimin 2009-11-17 12:50:03 11 */ 12 public class TestBigDecimal { 13 14 public static void main(String[] args) { 15 System.out.println("------------构造BigDecimal-------------"); 16 //从char[]数组来创建BigDecimal 17 BigDecimal bd1 = new BigDecimal("123456789.123456888".toCharArray(), 4, 12); 18 System.out.println("bd1=" + bd1); 19 //从char[]数组来创建BigDecimal 20 BigDecimal bd2 = new BigDecimal("123456789.123456111133333213".toCharArray(), 4, 18, MathContext.DECIMAL128); 21 System.out.println("bd2=" + bd2); 22 //从字符串创建BigDecimal 23 BigDecimal bd3 = new BigDecimal("123456789.123456111133333213"); 24 System.out.println("bd3=" + bd3); 25 //从字符串创建BigDecimal,3是有效数字个数 26 BigDecimal bd4 = new BigDecimal("88.456", new MathContext(3, RoundingMode.UP)); 27 System.out.println("bd4=" + bd4); 28 System.out.println("------------使用BigDecimal-------------"); 29 System.out.println("bd1+bd2=" + bd1.add(bd2)); 30 System.out.println("bd1+bd2=" + bd1.add(bd2, new MathContext(24, RoundingMode.UP))); 31 System.out.println("bd1-bd2=" + bd1.subtract(bd2).toPlainString()); 32 System.out.println("bd1-bd2=" + bd1.subtract(bd2, new MathContext(24, RoundingMode.UP)).toPlainString()); 33 System.out.println("bd1*bd2=" + bd1.multiply(bd2)); 34 System.out.println("bd1*bd2=" + bd1.multiply(bd2, new MathContext(24, RoundingMode.UP))); 35 System.out.println("bd1/bd4=" + bd1.divideToIntegralValue(bd4)); 36 System.out.println("bd1/bd4=" + bd1.divideToIntegralValue(bd4, new MathContext(24, RoundingMode.UP))); 37 System.out.println("bd1末位数据精度=" + bd1.ulp()); 38 System.out.println("bd2末位数据精度=" + bd2.ulp()); 39 System.out.println("bd2末位数据精度=" + bd2.ulp().toPlainString()); 40 System.out.println("bd1符号:" + bd1.signum()); 41 System.out.println("bd4的标度:" + bd4.scale()); 42 } 43 }
运行结果:
------------构造BigDecimal-------------
bd1=56789.123456
bd2=56789.123456111133
bd3=123456789.123456111133333213
bd4=88.5
------------使用BigDecimal-------------
bd1+bd2=113578.246912111133
bd1+bd2=113578.246912111133
bd1-bd2=-0.000000111133
bd1-bd2=-0.000000111133
bd1*bd2=3225004542.907120529593035648
bd1*bd2=3225004542.90712052959304
bd1/bd4=641.00000
bd1/bd4=641.00000
bd1末位数据精度=0.000001
bd2末位数据精度=1E-12
bd2末位数据精度=0.000000000001
bd1符号:1
bd4的标度:1
Process finished with exit code 0
bd1=56789.123456
bd2=56789.123456111133
bd3=123456789.123456111133333213
bd4=88.5
------------使用BigDecimal-------------
bd1+bd2=113578.246912111133
bd1+bd2=113578.246912111133
bd1-bd2=-0.000000111133
bd1-bd2=-0.000000111133
bd1*bd2=3225004542.907120529593035648
bd1*bd2=3225004542.90712052959304
bd1/bd4=641.00000
bd1/bd4=641.00000
bd1末位数据精度=0.000001
bd2末位数据精度=1E-12
bd2末位数据精度=0.000000000001
bd1符号:1
bd4的标度:1
Process finished with exit code 0
最后回顾下本文所涉及的API范围:
java.math包:
掌握了构造方式,和常见的数学运算,在开发中一般够用了。