Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:40:33+00:00 2026-06-09T14:40:33+00:00

I am performing a simple multiplication with BigDecimal and I have found some strange

  • 0

I am performing a simple multiplication with BigDecimal and I have found some strange behaviour when multiplying by zero (multiplying by zero is correct in this use-case).

Basic maths tells me that anything multiplied by zero will equal zero (see:Zero Product Property and Multiplication Properties)

However, the following code will consistently fail with the same error:

assertEquals(new BigDecimal(0), new BigDecimal(22.3).multiply(new BigDecimal(0)));
java.lang.AssertionError: 
Expected :0
Actual   :0E-48

Is this an inaccuracy with BigDecimal or is there some niche branch of maths that I’m missing somewhere?

Notes: JDK 1.6.0_27 running in IntelliJ 11

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:40:34+00:00Added an answer on June 9, 2026 at 2:40 pm

    You can’t use the equals() method to compare BigDecimals, like this assertion does. That is because this equals function will compare the scale. If the scale is different, equals() will return false, even if they are the same number mathematically.

    You can however use compareTo() to do what you want:

    As @assylias points out, you should also use the new BigDecimal("22.3") constructor to avoid double precision issues.

    BigDecimal expected = BigDecimal.ZERO;
    BigDecimal actual = new BigDecimal("22.3").multiply(BigDecimal.ZERO);
    assertEquals(0, expected.compareTo(actual));
    

    There is also a method called signum(), that returns -1, 0 or 1 for negative, zero, and positive. So you can also test for zero with

    assertEquals(0, actual.signum());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm performing matrix multiplication with this simple algorithm. To be more flexible I used
I use JFreeChart to display some simple data in a column chart. I have
Say we have stored procedure(s) performing simple operations like this: CREATE PROCEDURE [dbo].[AddNewAuthorReturnID] (
Okay, this is following on from my previous question reguarding performing a simple ajax
While performing some upcoming maintenance, I'm going to have to redirect all site traffic
When performing many inserts into a database I would usually have code like this:
I'm performing a while loop in C#, this is going through some records being
Simple question: Which is the most efficient way of performing a substituion like: Some
I have a simple html page with the purpose of performing changes to an
So I have this Java program that I use to munch through several terabytes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.