Java5:BigInteger、BigDecimal详解
生活随笔
收集整理的這篇文章主要介紹了
Java5:BigInteger、BigDecimal详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
從Java4到Java5,Java對BigInteger、BigDecimal兩個類功能一直再做擴展與改進。主要原因是這兩個數據類型很重要,在高精度的計算中全靠這兩個數據類型了。BigInteger和BigDecimal分別表示任意精度的整數與浮點數。 本文中不在追溯各個版本的變化,只看Java5中兩個類的使用。 一、java.math.BigInteger 不可變的任意精度的整數。 此類的用法比較簡單些,也不存在舍入等操作。 package?lavasoft;?
import?java.math.BigInteger;?
import?java.util.Random;?
/**?
* 測試BigInteger?
*?
* @author leizhimin 2009-11-17 12:49:41?
*/?
public?class?TestBigInteger {?
????????public?static?void?main(String[] args) {?
????????????????System.out.println("-------------------構造BigInteger---------------------");?
????????????????//通過byte數組來創建BigInteger?
????????????????BigInteger bi1 =?new?BigInteger(new?byte[]{1, 1});?
????????????????System.out.println("bi1="?+ bi1.toString());?
????????????????//創建帶符號的BigInteger?
????????????????BigInteger bi2 =?new?BigInteger(-1,?new?byte[]{1, 1});?
????????????????System.out.println("bi2="?+ bi2.toString());?
????????????????//創建帶符號的BigInteger隨機數?
????????????????BigInteger bi3 =?new?BigInteger(128, 20,?new?Random());?
????????????????System.out.println("bi3="?+ bi3.toString());?
????????????????//通過10進制字符串創建帶符號的BigInteger?
????????????????BigInteger bi4 =?new?BigInteger("12342342342342123423423412341");?
????????????????System.out.println("bi4="?+ bi4.toString());?
????????????????//通過10進制字符串創建帶符號的BigInteger?
????????????????BigInteger bi5 =?new?BigInteger("88888888888888888888888888888", Character.digit('a', 33));?
????????????????System.out.println("bi5="?+ bi5.toString());?
????????????????System.out.println("BigInteger的常量:");?
????????????????System.out.println("BigInteger.ZERO="?+ BigInteger.ZERO);?
????????????????System.out.println("BigInteger.ONE="?+ BigInteger.ONE);?
????????????????System.out.println("BigInteger.TEN="?+ BigInteger.TEN);?
????????????????System.out.println("-------------------使用BigInteger---------------------");?
????????????????System.out.println("bi1的相反數="?+ bi1.negate());?
????????????????System.out.println("bi1的相反數="?+ bi1.negate());?
????????????????System.out.println("bi1+bi2="?+ bi1.add(bi2));?
????????????????System.out.println("bi1-bi2="?+ bi1.subtract(bi2));?
????????????????System.out.println("bi1*bi2="?+ bi1.multiply(bi2));?
????????????????System.out.println("bi1/bi2="?+ bi1.divide(bi2));?
????????????????System.out.println("bi1的10次方="?+ bi1.pow(10));?
????????????????System.out.println("bi1的10次方="?+ bi1.pow(1));?
????????????????BigInteger[] bx = bi4.divideAndRemainder(bi1);?
????????????????System.out.println(">>>:bx[0]="?+ bx[0] +?",bx[1]="?+ bx[1]);?
????????????????System.out.println("bi2的絕對值="?+ bi2.abs());?
????????}?
} 運行結果: -------------------構造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?
二、java.math.BigDecimal 不可變的、任意精度的有符號十進制數。與之相關的還有兩個類: java.math.MathContext: 該對象是封裝上下文設置的不可變對象,它描述數字運算符的某些規則,如數據的精度,舍入方式等。 java.math.RoundingMode:這是一種枚舉類型,定義了很多常用的數據舍入方式。 這個類用起來還是很比較復雜的,原因在于舍入模式,數據運算規則太多太多,不是數學專業出身的人看著中文API都難以理解,這些規則在實際中使用的時候在翻閱都來得及。 package?lavasoft;?
import?java.math.BigDecimal;?
import?java.math.MathContext;?
import?java.math.RoundingMode;?
/**?
* 測試BigDecimal?
*?
* @author leizhimin 2009-11-17 12:50:03?
*/?
public?class?TestBigDecimal {?
????????public?static?void?main(String[] args) {?
????????????????System.out.println("------------構造BigDecimal-------------");?
????????????????//從char[]數組來創建BigDecimal?
????????????????BigDecimal bd1 =?new?BigDecimal("123456789.123456888".toCharArray(), 4, 12);?
????????????????System.out.println("bd1="?+ bd1);?
????????????????//從char[]數組來創建BigDecimal?
????????????????BigDecimal bd2 =?new?BigDecimal("123456789.123456111133333213".toCharArray(), 4, 18, MathContext.DECIMAL128);?
????????????????System.out.println("bd2="?+ bd2);?
????????????????//從字符串創建BigDecimal?
????????????????BigDecimal bd3 =?new?BigDecimal("123456789.123456111133333213");?
????????????????System.out.println("bd3="?+ bd3);?
????????????????//從字符串創建BigDecimal,3是有效數字個數?
????????????????BigDecimal bd4 =?new?BigDecimal("88.456",?new?MathContext(3, RoundingMode.UP));?
????????????????System.out.println("bd4="?+ bd4);?
????????????????System.out.println("------------使用BigDecimal-------------");?
????????????????System.out.println("bd1+bd2="?+ bd1.add(bd2));?
????????????????System.out.println("bd1+bd2="?+ bd1.add(bd2,?new?MathContext(24, RoundingMode.UP)));?
????????????????System.out.println("bd1-bd2="?+ bd1.subtract(bd2).toPlainString());?
????????????????System.out.println("bd1-bd2="?+ bd1.subtract(bd2,?new?MathContext(24, RoundingMode.UP)).toPlainString());?
????????????????System.out.println("bd1*bd2="?+ bd1.multiply(bd2));?
????????????????System.out.println("bd1*bd2="?+ bd1.multiply(bd2,?new?MathContext(24, RoundingMode.UP)));?
????????????????System.out.println("bd1/bd4="?+ bd1.divideToIntegralValue(bd4));?
????????????????System.out.println("bd1/bd4="?+ bd1.divideToIntegralValue(bd4,?new?MathContext(24, RoundingMode.UP)));?
????????????????System.out.println("bd1末位數據精度="?+ bd1.ulp());
import?java.math.BigInteger;?
import?java.util.Random;?
/**?
* 測試BigInteger?
*?
* @author leizhimin 2009-11-17 12:49:41?
*/?
public?class?TestBigInteger {?
????????public?static?void?main(String[] args) {?
????????????????System.out.println("-------------------構造BigInteger---------------------");?
????????????????//通過byte數組來創建BigInteger?
????????????????BigInteger bi1 =?new?BigInteger(new?byte[]{1, 1});?
????????????????System.out.println("bi1="?+ bi1.toString());?
????????????????//創建帶符號的BigInteger?
????????????????BigInteger bi2 =?new?BigInteger(-1,?new?byte[]{1, 1});?
????????????????System.out.println("bi2="?+ bi2.toString());?
????????????????//創建帶符號的BigInteger隨機數?
????????????????BigInteger bi3 =?new?BigInteger(128, 20,?new?Random());?
????????????????System.out.println("bi3="?+ bi3.toString());?
????????????????//通過10進制字符串創建帶符號的BigInteger?
????????????????BigInteger bi4 =?new?BigInteger("12342342342342123423423412341");?
????????????????System.out.println("bi4="?+ bi4.toString());?
????????????????//通過10進制字符串創建帶符號的BigInteger?
????????????????BigInteger bi5 =?new?BigInteger("88888888888888888888888888888", Character.digit('a', 33));?
????????????????System.out.println("bi5="?+ bi5.toString());?
????????????????System.out.println("BigInteger的常量:");?
????????????????System.out.println("BigInteger.ZERO="?+ BigInteger.ZERO);?
????????????????System.out.println("BigInteger.ONE="?+ BigInteger.ONE);?
????????????????System.out.println("BigInteger.TEN="?+ BigInteger.TEN);?
????????????????System.out.println("-------------------使用BigInteger---------------------");?
????????????????System.out.println("bi1的相反數="?+ bi1.negate());?
????????????????System.out.println("bi1的相反數="?+ bi1.negate());?
????????????????System.out.println("bi1+bi2="?+ bi1.add(bi2));?
????????????????System.out.println("bi1-bi2="?+ bi1.subtract(bi2));?
????????????????System.out.println("bi1*bi2="?+ bi1.multiply(bi2));?
????????????????System.out.println("bi1/bi2="?+ bi1.divide(bi2));?
????????????????System.out.println("bi1的10次方="?+ bi1.pow(10));?
????????????????System.out.println("bi1的10次方="?+ bi1.pow(1));?
????????????????BigInteger[] bx = bi4.divideAndRemainder(bi1);?
????????????????System.out.println(">>>:bx[0]="?+ bx[0] +?",bx[1]="?+ bx[1]);?
????????????????System.out.println("bi2的絕對值="?+ bi2.abs());?
????????}?
} 運行結果: -------------------構造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?
二、java.math.BigDecimal 不可變的、任意精度的有符號十進制數。與之相關的還有兩個類: java.math.MathContext: 該對象是封裝上下文設置的不可變對象,它描述數字運算符的某些規則,如數據的精度,舍入方式等。 java.math.RoundingMode:這是一種枚舉類型,定義了很多常用的數據舍入方式。 這個類用起來還是很比較復雜的,原因在于舍入模式,數據運算規則太多太多,不是數學專業出身的人看著中文API都難以理解,這些規則在實際中使用的時候在翻閱都來得及。 package?lavasoft;?
import?java.math.BigDecimal;?
import?java.math.MathContext;?
import?java.math.RoundingMode;?
/**?
* 測試BigDecimal?
*?
* @author leizhimin 2009-11-17 12:50:03?
*/?
public?class?TestBigDecimal {?
????????public?static?void?main(String[] args) {?
????????????????System.out.println("------------構造BigDecimal-------------");?
????????????????//從char[]數組來創建BigDecimal?
????????????????BigDecimal bd1 =?new?BigDecimal("123456789.123456888".toCharArray(), 4, 12);?
????????????????System.out.println("bd1="?+ bd1);?
????????????????//從char[]數組來創建BigDecimal?
????????????????BigDecimal bd2 =?new?BigDecimal("123456789.123456111133333213".toCharArray(), 4, 18, MathContext.DECIMAL128);?
????????????????System.out.println("bd2="?+ bd2);?
????????????????//從字符串創建BigDecimal?
????????????????BigDecimal bd3 =?new?BigDecimal("123456789.123456111133333213");?
????????????????System.out.println("bd3="?+ bd3);?
????????????????//從字符串創建BigDecimal,3是有效數字個數?
????????????????BigDecimal bd4 =?new?BigDecimal("88.456",?new?MathContext(3, RoundingMode.UP));?
????????????????System.out.println("bd4="?+ bd4);?
????????????????System.out.println("------------使用BigDecimal-------------");?
????????????????System.out.println("bd1+bd2="?+ bd1.add(bd2));?
????????????????System.out.println("bd1+bd2="?+ bd1.add(bd2,?new?MathContext(24, RoundingMode.UP)));?
????????????????System.out.println("bd1-bd2="?+ bd1.subtract(bd2).toPlainString());?
????????????????System.out.println("bd1-bd2="?+ bd1.subtract(bd2,?new?MathContext(24, RoundingMode.UP)).toPlainString());?
????????????????System.out.println("bd1*bd2="?+ bd1.multiply(bd2));?
????????????????System.out.println("bd1*bd2="?+ bd1.multiply(bd2,?new?MathContext(24, RoundingMode.UP)));?
????????????????System.out.println("bd1/bd4="?+ bd1.divideToIntegralValue(bd4));?
????????????????System.out.println("bd1/bd4="?+ bd1.divideToIntegralValue(bd4,?new?MathContext(24, RoundingMode.UP)));?
????????????????System.out.println("bd1末位數據精度="?+ bd1.ulp());
總結
以上是生活随笔為你收集整理的Java5:BigInteger、BigDecimal详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 诺安成长混合属于什么板块基金
- 下一篇: Lordstown 宣布暂停生产和交付全