Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Checking for a whole number

1. ## Checking for a whole number

Is there anyway I can check if a number is a whole number?

I am retrieving images from a database and on every third image I want to start a new row in the table displaying them. I have set \$i to be 0 and increased it with every reiteration of the loop. After having increased it I want to check if \$i divided by 3 produces a whole number or not.

How do I do this?

Thanks
WillowFae

• If you want the whole number every time:

round(\$variable, 0);

1.3423
would return
1

IF, you just want to see if its a full number or has a .

\$blah = explode("\.", \$variable);
if \$blah[1] == "" then
the number is whole
else then
the number is not whole
end if
or something

• if(eregi("\.", \$i))
{
echo "not a whole number";
}
else
{
echo "is a whole number";
}

if \$i has a "." it will not be considered a whole number.

• or you can just use

<?php
round(\$i);
?>

which will round the number to a whole number.

• Ooh, thanks for those. Just what I wanted - the checking option, not the rounding up one.

• Originally posted by coffeedemon

if \$i has a "." it will not be considered a whole number. [/B]
Ahh, but mathematically speaking, a whole number is...

"a positive integer", so

-1 although it would pass your code, is not a whole number. Neither is 0. So you would need to do an additional test to see if the number is > 0. If so, then it's whole.

Just getting picky

• ## Validate Whole Number in PHP

Hi Friends,
There is a Better and Faster Method than preg_match for whole number
validation. that is : ctype_digit().... It take only String parameter , so make
our input as String First like eg:

\$var = (string)\$value;

then You can Check like this:

if (ctype_digit(\$var) ){
print "whole number\n";
}else{
print "not whole number\n";
}

ok Be happy , if any clarification Revert to me
anes.pa@gmail.com

Bye Anes

• ...the hell? This is a 7 year old post!

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•