Recommended usage:

public static void main(String[] args) { double d1 = 1; Double d2 = 20.2; Double d3 = 300.03; double d4 = d1 + d2 +d3; System.out.println("double result: "+ d4); / / System. 321.22999999999996 out. Print (" = = = = = = = = = = = = = = = = = = = \ n "); BigDecimal b1 = new BigDecimal(Double.toString(d1)); BigDecimal b2 = new BigDecimal(Double.toString(d2)); BigDecimal b3 = new BigDecimal(Double.toString(d3)); b2=b1.add(b2).add(b3); System.out.println("BigDecimal result: "+b2); / / 321.23}Copy the code

1. Compare floating point equality: the difference between the two is less than 10E-6

2.new BigDecimal(Double d); It is not reliable and can also cause loss of accuracy

Reference blog:

Blog.csdn.net/yacolspace/…