Java 常用對象-BigDecimal

來源:互聯網
上載者:User

標籤:數值   .com   構造   val   組成   color   十進位   表示   分享   

2017-11-02 22:03:14

BigDecimal類:不可變的、任意精度的有符號十進位數。BigDecimal 由任意精度的整數非標度值 和 32 位的整數標度 (scale) 組成。如果為零或正數,則標度是小數點後的位元。如果為負數,則將該數的非標度值乘以 10 的負 scale 次冪。因此,BigDecimal 表示的數值是 (unscaledValue × 10-scale)。

BigDecimal 類提供以下操作:算術、標度操作、舍入、比較、雜湊演算法和格式轉換。toString() 方法提供 BigDecimal 的規範表示形式。

        System.out.println(0.09+0.01);        System.out.println(1.0-0.32);        System.out.println(1.015*100);//        0.09999999999999999//        0.6799999999999999//        101.49999999999999

Java中float和double儲存會有精度問題,所以引入了BigDecimal類。

*構造方法:

最好使用字串進行初始化。

*常用方法:

 

BigDecimal bd1=new BigDecimal("0.01");System.out.println(bd1.add(new BigDecimal("0.09")));//0.10

 

Java 常用對象-BigDecimal

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.