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

  • Home
  • SEARCH
  • 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 8474951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:45:51+00:00 2026-06-10T17:45:51+00:00

I needed to check if a BigDecimal instance is a multiple of another BigDecimal

  • 0

I needed to check if a BigDecimal instance is a multiple of another BigDecimal instance. Since I am restricted to Java 1.4, I couldn’t make use of BigDecimal.remainder(...).

I came up with the following code:

/**
 * Returns <code>true</code> if <code>multiple</code> is a multiple of <code>base</code>.
 *
 * @param multiple
 * @param base
 * @return
 */
public static boolean isMultipleOf(final BigDecimal multiple, final BigDecimal base)
{
    if (multiple.compareTo(base) == 0)
        return true;

    try
    {
        multiple.divide(base, 0, BigDecimal.ROUND_UNNECESSARY);
        return true;
    }
    catch(ArithmeticException e)
    {
        return false;
    }
}

The code assumes that rounding is unnecessary only if the quotient of multiple divided by base is an integer. Is this assumption correct?

Edit
Incorporated the hint by Lando to explicitly set the scale of the quotient to 0. Otherwise the following call would have wrongly returned true:

isMultipleOf(new BigDecimal("10.25"), new BigDecimal("5"));

Edit
Wrote a JUnit test and found out I have to take care of the special case where both multiple and base are 0. So I added a test for equality to the code above.

  • 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-10T17:45:53+00:00Added an answer on June 10, 2026 at 5:45 pm

    The javadoc says…

    Throws: ArithmeticException – if divisor==0, or roundingMode==ROUND_UNNECESSARY and this.scale() is insufficient to represent the result of the division exactly.

    So the assumption seems correct that an ArithmeticException will occur if the scale() is not sufficient; i.e. remainder is non-zero.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I needed to check the memory stats of objects I use in python. I
According to following article, I can create a simple socket in Java to check
I want the program to check how many large boxes are needed, then use
Which is, on average, faster - check the value then, if needed, assign, or
I was working with ArrayWritable , at some point I needed to check how
I needed to rewrite lots of asp.net membership stuff since it did not meet
I needed to check if a picture is included in a big set of
In a programming scenario, I needed to check if my GWT textbox was focused
Possible Duplicate: Get Information about a SHA-1 commit object? I needed to check when
The goal is to check if a string is a URL, no regex needed.

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.