java float 加減精度問題

來源:互聯網
上載者:User
java float 加減精度問題在取這個欄位的時候轉換成BigDecimal就可以了同時,BigDecimal是可以設定精度的。float m = 12.22F;float c = 1.22F;BigDecimal b1 = new BigDecimal(Float.toString(m));BigDecimal b2 = new BigDecimal(Float.toString(c));System.out.println(m);System.out.println(c);Float add = b1.add(b2).floatValue();System.out.println("add=========" + add);Float less = b1.subtract(b2).floatValue();System.out.println("less=========" + less);Float multiply = b1.multiply(b2).floatValue();System.out.println("multiply=========" + multiply);//給divide設定精確的小數點,解決不整除報異常Float divide = b1.divide(b2,4, BigDecimal.ROUND_HALF_EVEN).floatValue();System.out.println("divide=========" + divide);===========================================BigDecimal不整除的一個異常java.lang.ArithmeticException: Non-terminating decimal expansion金額的資料類型是BigDecimal 通過BigDecimal的divide方法進行除法時當不整除,出現無限迴圈小數時,就會拋異常的,異常如下:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result. at java.math.BigDecimal.divide(Unknown Source)應用情境:一批中供客戶的單價是1000元/年,如果按月計算的話1000/12=83.3333333333.... 解決之道:就是給divide設定精確的小數點divide(xxxxx,2, BigDecimal.ROUND_HALF_EVEN) 在 java中, 四捨五入通過 BigDecimal 來實現。一定要注意:BigDecimal is Immutable。也就是跟String一樣,對前一個的修改,比如setScale(), add()等都會返回一個新的BigDecimal.四捨五入舍入模式是 BigDecimal.ROUND_HALF_UP BigDecimal定義了一下舍入模式,只有在作除法運算或四捨五入時才用到舍入模式,下面簡單介紹,詳細請查閱J2se API文檔static intROUND_CEILINGRounding mode to round towards positive infinity.向正無窮方向舍入static intROUND_DOWNRounding mode to round towards zero.向零方向舍入static intROUND_FLOORRounding mode to round towards negative infinity.向負無窮方向舍入static intROUND_HALF_DOWNRounding mode to round towards "nearest neighbor" unless both neighbors are equidistant,in which case round down.向(距離)最近的一邊舍入,除非兩邊(的距離)是相等,如果是這樣,向下舍入,例如1.55保留一位小數結果為1.5static intROUND_HALF_EVENRounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant,in which case, round towards the even neighbor.向(距離)最近的一邊舍入,除非兩邊(的距離)是相等,如果是這樣,如果保留位元是奇數,使用ROUND_HALF_UP,如果是偶數,使用ROUND_HALF_DOWNstatic intROUND_HALF_UPRounding mode to round towards "nearest neighbor" unless both neighbors are equidistant,in which case round up.向(距離)最近的一邊舍入,除非兩邊(的距離)是相等,如果是這樣,向上舍入, 1.55保留一位小數結果為1.6static intROUND_UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence norounding is necessary.計算結果是精確的,不需要舍入模式static intROUND_UPRounding mode to round away from zero.向遠離0的方向舍入===========================================如何應用Java的BigDecimal類雙精確度浮點型變數double可以處理16位有效數。在實際應用中,需要對更大或者更小的數進行運算和處理。Java在java.math包中提供的API類BigDecimal,用來對超過16位有效位的數進行精確的運算。表5.7中列出了BigDecimal類的主要構造器和方法。表5.7 BigDecimal類的主要構造器和方法構造器  描 述BigDecimal(int)建立一個具有參數所指定整數值的對象。BigDecimal(double)建立一個具有參數所指定雙精確度值的對象。BigDecimal(long)建立一個具有參數所指定長整數值的對象。BigDecimal(String)建立一個具有參數所指定以字串表示的數值的對象。續表方 法描 述add(BigDecimal)BigDecimal對象中的值相加,然後返回這個對象。subtract(BigDecimal)BigDecimal對象中的值相減,然後返回這個對象。multiply(BigDecimal)BigDecimal對象中的值相乘,然後返回這個對象。divide(BigDecimal)BigDecimal對象中的值相除,然後返回這個對象。toString()將BigDecimal對象的數值轉換成字串。doubleValue()將BigDecimal對象中的值以雙精確度數返回。floatValue()將BigDecimal對象中的值以單精確度數返回。longValue()將BigDecimal對象中的值以長整數返回。intValue()將BigDecimal對象中的值以整數返回。注意,由於一般數實值型別,例如double,不能準確地代表16位有效數以上的數字,在使用BigDecimal時,應用BigDecimal(String)構造器建立對象才有意義。另外,BigDecimal所建立的是對象,我們不能使用傳統的+、-、*、/等算術運算子直接對其對象進行數學運算,而必須調用其相對應的方法。方法中的參數也必須是BigDecimal的對象。構造器是類的特殊方法,專門用來建立對象,特別是帶有參數的對象。關於構造器概念和編寫技術,將在本書第6章詳細介紹。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.