Java BigDecimal precision problems
问题 I know the following behavior is an old problem, but still I don't understand. System.out.println(0.1 + 0.1 + 0.1); Or even though I use BigDecimal System.out.println(new BigDecimal(0.1).doubleValue() + new BigDecimal(0.1).doubleValue() + new BigDecimal(0.1).doubleValue()); Why this result is: 0.30000000000000004 instead of: 0.3 ? How can I solve this? 回答1: What you actually want is new BigDecimal("0.1") .add(new BigDecimal("0.1")) .add(new BigDecimal("0.1")); The new BigDecimal(double)