Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Best way to determine if a number is evenly divisible by a double?

807603Dec 11 2007 — edited Dec 11 2007
I need to determine if some number is divisible by some other number. A simple solution is:
public boolean isDivisibleBy(double dividend, double divisor)
{
   return dividend % divisor == 0;
}
Unfortunately this does not work if the divisor is not an integer (e.g. 1.0 % 0.1 results in 0.1).

I am trying to find a solution with good performance, but hopefully doesn't make the code too messy. I assume someone has solved this problem before, but apparently my Google-fu is weak. :-(

Anyone have a solution they'd care to share?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 8 2008
Added on Dec 11 2007
7 comments
1,568 views