gpt4 book ai didi

java.math.BigDecimal比较: the same value represented in different formats don't match

转载 作者:行者123 更新时间:2023-12-01 10:59:36 26 4
gpt4 key购买 nike

以下代码:

 BigDecimal a = new BigDecimal("8900");
BigDecimal b = new BigDecimal("8.9E+3");

System.out.println(a.equals(b));

打印错误。如果从数学角度来说这些数字是相等的,为什么会这样呢?

internal representation

最佳答案

您必须使用compareTo():

BigDecimal a = new BigDecimal("8900");
BigDecimal b = new BigDecimal("8.9E+3");

System.out.println(a.compareTo(b) == 0);

关于java.math.BigDecimal比较: the same value represented in different formats don't match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33440146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com