1. 程式人生 > >Java中的高精度整數和高精度小數

Java中的高精度整數和高精度小數

整數測試 div string pac 高精 int trac print log

在實際編碼中,會遇到很多高精度的事例,比如,在計算金錢的時候就需要保留高精度小數,這樣計算才不會有太大誤差:

在下面的代碼中,我們驗證了,當兩個float型的數字相加,得到的結果和我們的預期結果是有誤差的,為了減小和防止這種誤差的出現,我們需要使用BigInteger類和BigDecimal類來計算。

package com.ietree.base.number;

import java.math.BigDecimal;
import java.math.BigInteger;

public class BigIntegerTest {

    public static void main(String[] args) {
        
float f1 = 123.01f + 2.01f; // 預期輸出:125.02,實際輸出:125.020004 System.out.println(f1); // 預期輸出:125.02,實際輸出:125.02000000000001 System.out.println(123.01 + 2.01); System.out.println("==============================="); // 高精度整數測試 BigInteger bint1 = new BigInteger("125"); BigInteger bint2
= new BigInteger("999"); BigInteger tmp; // 相加 tmp = bint1.add(bint2); System.out.println("bint1 + bint2 = " + tmp); // 相減 tmp = bint2.subtract(bint1); System.out.println("bint2 - bint1 = " + tmp); // 相乘 tmp = bint1.multiply(bint2); System.out.println(
"bint1 * bint2 = " + tmp); // 相除 tmp = bint2.divide(bint1); System.out.println("bint2 / bint1 = " + tmp); // 求余數 tmp = bint2.remainder(bint1); System.out.println("bint2 % bint1 = " + tmp); // 求次方 tmp = bint2.pow(2); System.out.println("bint2的二次方 = " + tmp); System.out.println("======================================"); // 高精度小數測試 BigDecimal bd1 = new BigDecimal(123.01); BigDecimal bd2 = new BigDecimal(2.01); BigDecimal bd; // 相加 bd = bd1.add(bd2); System.out.println("bd1 + bd2 = " + bd); // 相減 bd = bd1.subtract(bd2); System.out.println("bd2 - bd1 = " + bd); // 相乘 bd = bd1.multiply(bd2); System.out.println("bd1 * bd2 = " + bd); // 相除 // bd = bd1.divide(bd2); bd = bd1.divide(new BigDecimal(2.0)); System.out.println("bd1 / 2.0 = " + bd); // 求余數 bd = bd1.remainder(bd2); System.out.println("bd2 % bd1 = " + bd); // 求次方 bd = bd1.pow(3); System.out.println("bd2的三次方 = " + bd); System.out.println("======================================"); // 四舍五入保留小數位數 BigDecimal bd3 = new BigDecimal(123.01).setScale(5,5); System.out.println("bd3 = " + bd3); } }

Java中的高精度整數和高精度小數