2019独角兽企业重金招聘Python工程师标准>>>
BigDecimal oldPrice=new BigDecimal(50);
BigDecimal newPrice=new BigDecimal(50.00);
out.println("直接比较"+oldPrice.equals(newPrice));
out.println("直接比较"+newPrice.equals(oldPrice));
out.println("比较float值:"+(oldPrice.floatValue()==newPrice.floatValue()));
out.println("比较double值:"+(oldPrice.doubleValue()==newPrice.doubleValue()));
BigDecimal oldPrice1=new BigDecimal("50.1");
BigDecimal newPrice1=new BigDecimal("50.10");
out.println("直接比较"+oldPrice1.equals(newPrice1));
out.println("直接比较"+newPrice1.equals(oldPrice1));
out.println("比较float值:"+(oldPrice1.floatValue()==newPrice1.floatValue()));
out.println("比较double值:"+(oldPrice1.doubleValue()==newPrice1.doubleValue()));
输出结果
直接比较true
直接比较true
比较float值:true
比较double值:true
直接比较false
直接比较false
比较float值:true
比较double值:true
当从后台接受BigDecimal是从string字符串转成的,所以会保留小数点.如果原价是3.90,新价是3.9,则认为它两不相等.