double相乘少一分的问题,BigDecimal参数传小数也会出问题
public static void main(String[] args) { System.out.println(String.valueOf((int) (19.9 * 100))); //1989 double aa = 19.9; double bb = 100; System.out.println(aa*bb); //1989.9999999999998 BigDecimal v1 = new BigDecimal("19.9"); BigDecimal v2 = new BigDecimal("100"); Double v = v1.multiply(v2).doubleValue(); System.out.println(v.intValue()); //1990 System.out.println(v); //1990.0 BigDecimal b1 = new BigDecimal(19.9); BigDecimal b2 = new BigDecimal(100); System.out.println(b1); //19.89999999999999857891452847979962825775146484375 Double b = b1.multiply(b2).doubleValue(); System.out.println(b.intValue()); //1989 System.out.println(b); //1989.9999999999998 }
结论就是小数相乘,要使用BigDecimal且构造参数传入字符串