System.
out
.println(0.15d - 0.05d);
// 0.09999999999999999
System.
out
.println(2.08f - 3.7f);
// -1.6200001
System.
out
.println(13.2d * 7);
//92.39999999999999
解决方案:
方案一:
BigDecimal b1 =
new
BigDecimal(String.valueOf(13.2d));
BigDecimal b2 =
new
BigDecimal(String.valueOf(7));
//add加,subtract减,multiply乘,divide除
double
value = b1.multiply(b2).doubleValue();
System.
out
.println(value);
//92.4
方案二:
private
static
final
DecimalFormat
AA
=
new
DecimalFormat(
"##.00"
);
private
static
final
DecimalFormat
BB
=
new
DecimalFormat(
"##.##"
);
String value1 =
AA
.format(13.2d * 7);
String value2 =
BB
.format(13.2d * 7);
System.
out
.println(value1);
//92.40
System.
out
.println(value2);
//92.4
方案三:
NumberFormat numberFormat = NumberFormat.getInstance();
String value = numberFormat.format(13.2d * 7);
System.
out
.println(value);
//92.4