Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigDecimal equals problem #904

Closed
ybank opened this issue Aug 22, 2016 · 0 comments · Fixed by #2364
Closed

BigDecimal equals problem #904

ybank opened this issue Aug 22, 2016 · 0 comments · Fixed by #2364
Labels

Comments

@ybank
Copy link

ybank commented Aug 22, 2016

Dear developers, it looks like the primitive's equals to did not handle BigDecimal's comparison. The following test will fail:

  public void testUnequalDecimals() {
      JsonPrimitive small = new JsonPrimitive(1.0);
      JsonPrimitive large = new JsonPrimitive(2.0);
      assertFalse("small = large", small.equals(large));

      BigDecimal dmax = BigDecimal.valueOf(Double.MAX_VALUE);
      JsonPrimitive smallBD =        // dmax + 100.0
          new JsonPrimitive(dmax.add(new BigDecimal("100.0")));
      JsonPrimitive largeBD =        // dmax + 200.0
          new JsonPrimitive(dmax.add(new BigDecimal("200.0")));
      assertFalse("small = large", smallBD.equals(largeBD));
  }

Could you consider a fix for this, so it can support big decimal comparisons, too?

Thanks!

OmarAlJarrah added a commit to OmarAlJarrah/gson that referenced this issue Jul 14, 2022
@Marcono1234 Marcono1234 added the bug label Aug 2, 2022
MaicolAntali added a commit to MaicolAntali/gson that referenced this issue Apr 1, 2023
Adds test to check if the equals work with BigDecimals. Code snippet from issue google#904
eamonnmcmanus pushed a commit that referenced this issue Feb 9, 2024
* Adds support to `BigDecimal`

Adds to the JsonPrimitive#equals the possibility to support BigDecimal

* Adds test

Adds test to check if the equals work with BigDecimals. Code snippet from issue #904

* Implements review comments

Replaces the `.equals` method with the `compareTo` in the `JsonPrimitive#equals`

Change the ternary operator from `||` to `&&` so we are sure that both are `BigDecimal`

Implements tests

* Changes to follow the google-style-guide

* implements review comment

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>

* Fixes the `OperatorPrecedence` warn

* Implements code improvements

- Extracts `thisAsDouble` & `otherAsDouble` variables to avoid double functions calls.

- Adds a comment to improve the code readability.

* Implements `BigDecimal` check in the `JsonPrimitive.equals()`

* Formats the code with `spotless:apply`

---------

Co-authored-by: Marcono1234 <Marcono1234@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants