• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java: How to check if a double has a decimal potion?

statik213

Golden Member
I'm doing this right now:

double d;

if(d > (long) d)
// has a decimal potion
else
// is an integer

is there a better a way?
 
I would have done this:

double d;
if((int)d == d){
//stuff
}
else{
//other stuff
}

Your code doesn't work with negative numbers.
 
You can use modulus operator (%) with doubles as well.

double d;
if (d % 1.0 > 0)
// We have decimal part.
 
Originally posted by: notfred
I would have done this:

double d;
if((int)d == d){
//stuff
}
else{
//other stuff
}

Your code doesn't work with negative numbers.

ok, didnt think of that
 
Back
Top