Skip to content

[DOCUMENTATION] Add BigDecimal explanation to EqAndHashCode feature page #3182

Description

@rzwitserloot

BigDecimal has a wonky definition of equality, for example 1E2 is not equal to 100 in the sense that bigDecimalA.equals(bigDecimalB) would return false because they have different scales. Whilst we defer to BD's authors - that is evidently how they intend for equality to work - it's confusing in any case (not our fault), and often folks want to define equality as: Does it represent the same value?

There IS a way to accomplish this:

@EqualsAndHashCode.Include
private BigDecimal nameOfBigDecimalField() {
  return nameOfBigDecimalField..stripTrailingZeros();
}

This should be named on the feature page of @EqualsAndHashCode; probably in the smallprint, and including that snippet or at least a link to it.

See issue #1260 where @wacker confirmed that this 'trick' works.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationAddressing this item primarily involves writing docs / updating the website

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions