...

# Checking for a whole number

WillowFae
10-27-2003, 05:34 PM
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

Acecool
10-27-2003, 05:53 PM
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

coffeedemon
10-27-2003, 05:56 PM
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.

coffeedemon
10-27-2003, 06:02 PM
or you can just use

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

which will round the number to a whole number.

WillowFae
10-27-2003, 06:05 PM
Ooh, thanks for those. Just what I wanted - the checking option, not the rounding up one.

:)

bcarl314
10-27-2003, 07:09 PM
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 :D

amskape
06-17-2010, 03:06 PM
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

tomws
06-17-2010, 03:30 PM
...the hell? This is a 7 year old post!