BigDecimal equals() 与 CompareTo()

发布于 2024-11-26 06:56:17 字数 836 浏览 3 评论 0原文

考虑简单的测试类:

import java.math.BigDecimal;

/**
 * @author The Elite Gentleman
 *
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BigDecimal x = new BigDecimal("1");
        BigDecimal y = new BigDecimal("1.00");
        System.out.println(x.equals(y));
        System.out.println(x.compareTo(y) == 0 ? "true": "false");
    }

}

您可以(有意识地)说x等于y(不是对象引用),但是当您运行程序时,会显示以下结果:

false
true

问题:BigDecimal中的compareTo()equals()有什么区别,compareTo可以判断>x 等于是吗?

PS:我看到 BigDecimal 在 equals() 方法上有一个 inflate() 方法。 inflate() 实际上做了什么?

Consider the simple test class:

import java.math.BigDecimal;

/**
 * @author The Elite Gentleman
 *
 */
public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BigDecimal x = new BigDecimal("1");
        BigDecimal y = new BigDecimal("1.00");
        System.out.println(x.equals(y));
        System.out.println(x.compareTo(y) == 0 ? "true": "false");
    }

}

You can (consciously) say that x is equal to y (not object reference), but when you run the program, the following result shows:

false
true

Question: What's the difference between compareTo() and equals() in BigDecimal that compareTo can determine that x is equal to y?

PS: I see that BigDecimal has an inflate() method on equals() method. What does inflate() do actually?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海里的那抹蓝 2024-12-03 06:56:17

答案在 equals() 方法的 JavaDoc

compareTo,仅当两个 BigDecimal 对象的值和小数位数相等时,此方法才认为它们相等(因此 2.0 不等于2.00(用此方法比较)。

换句话说:equals() 检查 BigDecimal 对象在每个方面是否完全相同。 compareTo() “仅”比较它们的数值。

至于为什么 equals() 的行为方式如此,这已经得到解答在此问题中

The answer is in the JavaDoc of the equals() method:

Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).

In other words: equals() checks if the BigDecimal objects are exactly the same in every aspect. compareTo() "only" compares their numeric value.

As to why equals() behaves this way, this has been answered in this SO question.

紫竹語嫣☆ 2024-12-03 06:56:17

我相信正确的答案是使两个数字(BigDecimals)具有相同的比例,然后我们可以决定它们的相等性。例如,这两个数相等吗?

1.00001 and 1.00002

嗯,这取决于规模。在 5 级(5 个小数点)上,不,它们不一样。但在较小的十进制精度(小数位数 4 及更低)上,它们被认为是相等的。
所以我建议让两个数字的比例相等然后进行比较。

I believe that the correct answer would be to make the two numbers (BigDecimals), have the same scale, then we can decide about their equality. For example, are these two numbers equal?

1.00001 and 1.00002

Well, it depends on the scale. On the scale 5 (5 decimal points), no they are not the same. but on smaller decimal precisions (scale 4 and lower) they are considered equal.
So I suggest make the scale of the two numbers equal and then compare them.

有深☉意 2024-12-03 06:56:17

我看到 BigDecimal 在 equals() 方法上有一个 inflate() 方法。 inflate() 实际上做了什么?

基本上,如果需要的话,inflate() 会调用 BigInteger.valueOf(intCompact),即它创建未缩放的值,该值存储为 BigInteger代码>long intCompact。如果您不需要 BigInteger 并且未缩放的值适合 long ,则 BigDecimal 似乎会尝试尽可能节省空间。

I see that BigDecimal has an inflate() method on equals() method. What does inflate() do actually?

Basically, inflate() calls BigInteger.valueOf(intCompact) if necessary, i.e. it creates the unscaled value that is stored as a BigInteger from long intCompact. If you don't need that BigInteger and the unscaled value fits into a long BigDecimal seems to try to save space as long as possible.

溺孤伤于心 2024-12-03 06:56:17

您还可以与双值进行比较

BigDecimal a= new BigDecimal("1.1"); BigDecimal b =new BigDecimal("1.1");
System.out.println(a.doubleValue()==b.doubleValue());

You can also compare with double value

BigDecimal a= new BigDecimal("1.1"); BigDecimal b =new BigDecimal("1.1");
System.out.println(a.doubleValue()==b.doubleValue());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文