Java Double 和 BigDecimal 互转

        // Double to BigDecimal
        BigDecimal b1 = new BigDecimal(3.14);
        BigDecimal b2 = new BigDecimal(Double.toString(3.14));
        BigDecimal b3 = BigDecimal.valueOf(-3.14);
        System.out.println("b1 = " + b1.toPlainString());
        System.out.println("b2 = " + b2.toPlainString());
        System.out.println("b3 = " + b3.toPlainString());
        //b1 = 3.140000000000000124344978758017532527446746826171875
        //b2 = 3.14
        //b3 = -3.14

        // BigDecimal to Double
        System.out.println("b1 double value = " + b1.doubleValue());
        System.out.println("b2 double value = " + b2.doubleValue());
        System.out.println("b3 double value = " + b3.doubleValue());
        //b1 double value = 3.14
        //b2 double value = 3.14
        //b3 double value = -3.14

你可能感兴趣的:(Java Double 和 BigDecimal 互转)