BigDecimal類實現任意精度的浮點運算,其有很多種構造方法,但常用的應該如下所示(個人推測): BigDecimal(BigInteger val) Translates a BigInteger into a BigDecimal. BigDecimal(double val) Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. BigDecimal(int val) Translates an int into a BigDecimal. BigDecimal(long val) Translates a long into a BigDecimal. BigDecimal(String val) Translates the string representation of a BigDecimal into a BigDecimal. 以上構造方法分別通過傳入大整數,雙精確度浮點數,整數,長整數,字串(不能傳入Float型)作為參數而構造BigDecimal 對象。參考API,上面構造方法還有對應的傳入MathContext對象作為參數如BigDecimal(BigInteger val, MathContext mc) ,MathContext類用於設定精度和舍入規則。 add(BigDecimal augend) Returns a BigDecimal whose value is (this + augend), and whose scale is max(this.scale(), augend.scale()). subtract(BigDecimal subtrahend) Returns a BigDecimal whose value is (this - subtrahend), and whose scale is max(this.scale(), subtrahend.scale()). multiply(BigDecimal multiplicand) Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()). divide(BigDecimal divisor) Returns a BigDecimal whose value is (this / divisor), and whose preferred scale is (this.scale() - divisor.scale()); if the exact quotient cannot be represented (because it has a non-terminating decimal expansion) an ArithmeticException is thrown. pow(int n) Returns a BigDecimal whose value is (thisn), The power is computed exactly, to unlimited precision. 以上方法返回大小數之和,差,積,商與冪。還可以制定舍入方式即roundingMode,通常使用的有ROUND_HALF_UP(四捨五入),ROUND_HALF_DOWN(向下取整),ROUND_DOWN,ROUND_UP。 在網上搜到很多使用BigDecimal類求圓周率的例子,其實在POJ上1001題即為求高精度運算。其程式碼如下: import java.util.Scanner; public class Pku1001 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); while (scan.hasNext()) { System.out.println(scan.nextBigDecimal().pow(scan.nextInt()) .stripTrailingZeros().toPlainString().replaceAll("^0", "")); } } } 這段代碼純調用API就解決該題,而且也讓我體會到Regex的強大。 ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー まで: BigDecimal對象的用法 在我現在進行的項目中用到這種類型,用它來表示金額。可這種類型非常精確,當你在資料庫裡插入一條資料是6.00時,在顯示時卻是6.000000000000000000000。在頁面顯示非常不美觀。在網上找了找,還真的找到瞭解決辦法拿來和大家分享一下,在以後做系統時可以用到。它不僅可以做到double和float,而且在商業計算上也能顯示它的作用。 java.math.BigDecimal。BigDecimal一共有4個夠造方法,讓我先來看看其中的兩種用法:
使用BigDecimal要用String來夠造,要做一個加法運算,需要先將兩個浮點數轉為String,然後夠造成BigDecimal,在其中一個上調用add方法,傳入另一個作為參數,然後把運算的結果(BigDecimal)再轉換為浮點數。 public static double add(double v1,double v2) public static double sub(double v1,double v2) public static double mul(double v1,double v2) public static double div(double v1,double v2) public static double div(double v1,double v2,int scale) public static double round(double v,int scale) 附錄 源檔案Arith.java: import java.math.BigDecimal; /** * 由於Java的簡單類型不能夠精確的對浮點數進行運算,這個工具類提供精 * 確的浮點數運算,包括加減乘除和四捨五入。 */ public class Arith{ //預設除法運算精度 private static final int DEF_DIV_SCALE = 10; //這個類不能執行個體化 private Arith(){ } /** * 提供精確的加法運算。 * @param v1 被加數 * @param v2 加數 * @return 兩個參數的和 */ public static double add(double v1,double v2){ BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.add(b2).doubleValue(); } /** * 提供精確的減法運算。 * @param v1 被減數 * @param v2 減數 * @return 兩個參數的差 */ public static double sub(double v1,double v2){ BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.subtract(b2).doubleValue(); } /** * 提供精確的乘法運算。 * @param v1 被乘數 * @param v2 乘數 * @return 兩個參數的積 */ public static double mul(double v1,double v2){ BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.multiply(b2).doubleValue(); } /** * 提供(相對)精確的除法運算,當發生除不盡的情況時,精確到 * 小數點以後10位,以後的數字四捨五入。 * @param v1 被除數 * @param v2 除數 * @return 兩個參數的商 */ public static double div(double v1,double v2){ return div(v1,v2,DEF_DIV_SCALE); } /** * 提供(相對)精確的除法運算。當發生除不盡的情況時,由scale參數指 * 定精度,以後的數字四捨五入。 * @param v1 被除數 * @param v2 除數 * @param scale 表示表示需要精確到小數點以後幾位。 * @return 兩個參數的商 */ public static double div(double v1,double v2,int scale){ if(scale<0){ throw new IllegalArgumentException( "The scale must be a positive integer or zero"); } BigDecimal b1 = new BigDecimal(Double.toString(v1)); BigDecimal b2 = new BigDecimal(Double.toString(v2)); return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue(); } /** * 提供精確的小數位四捨五入處理。 * @param v 需要四捨五入的數字 * @param scale 小數點後保留幾位 * @return 四捨五入後的結果 */ public static double round(double v,int scale){ if(scale<0){ throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue(); } }; Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1497357 |