package com.cyjt97.BD; import java.math.BigDecimal; import java.math.RoundingMode; //BigDecimal解决小数不精准问题 public class day { public static void main(String[] args) { BigDecimal bd1 = BigDecimal.valueOf(10.0); BigDecimal bd2 = BigDecimal.valueOf(3.0); // 第二位是精确到第几位 System.out.println(bd1.divide(bd2,2, RoundingMode.HALF_UP)); //3.33 System.out.println(bd1.divide(bd2,2, RoundingMode.UP)); //3.34 进一法 System.out.println(bd1.divide(bd2,2, RoundingMode.HALF_DOWN)); //3.33 BigDecimal res = bd1.divide(bd2,2,RoundingMode.HALF_UP); double s = res.doubleValue(); Math.abs(s); jj(); } private static void jj() { double num1 = 0.1; double num2 = 0.2; System.out.println(num1 + num2); // 推荐方法 BigDecimal BD1 = BigDecimal.valueOf(0.1); BigDecimal BD2 = BigDecimal.valueOf(0.2); // add加法 System.out.println(BD1.add(BD2));//0.3 // subtract减法 System.out.println(BD1.subtract(BD2));//-0.1 // multiply乘法 System.out.println(BD1.multiply(BD2));//0.02 // divide除法 System.out.println(BD1.divide(BD2));//0.5 } }
标签:java,进阶,System,41,BD1,divide,println,out,BigDecimal From: https://www.cnblogs.com/wencaiguagua/p/17227088.html