how to check if a number is whole number or contains a decimal

If you want to know if the result of dividing a $dividend by a $divisor is going to be a whole number or a number with a fractional portion, you can test that condition first:

if (my $remainder = $dividend % $divisor) {
    print "$dividend cannot be divided evenly by $divisor.",
          " There is a remainder of $remainder.\n";
else {
    print "$dividend is evenly divisible by $divisor,",
          " and the result is ", $divident / $divisor, "\n";

*EDIT: * This answer was posted in response to the original, un-edited question, which asked how to determine if the result of division contains a decimal portion. Now that the sense of the question changed toward date math, I agree that a DateTime solution is much better.

Browse More Popular Posts

Leave a Comment