views:

531

answers:

6

I have this PHP code:

$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;

What i want to know is, how to check whether $entityElementCount is a whole number (2, 6, ...) or partial (2.33, 6.2, ...).

Thank you!

+6  A: 
if(floor($number) == $number)
Chacha102
Try this, $number = (-(4.42-5))/0.29; After calculations $number = 2, but the condition above fails to tell that it is a whole number
spacemonkey
Try var_dump on $number. It's a float, which is why this isn't working. Floats are imprecise.
Alex JL
A: 
floor($entityElementCount) == $entityElementCount

This will be true if this is a whole number

Karaziox
+1  A: 
$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;
if (ctype_digit($entityElementCount) ){
    // (ctype_digit((string)$entityElementCount))  // as advised.
    print "whole number\n";
}else{
    print "not whole number\n";
}
ghostdog74
Does not work either
spacemonkey
Sorry I have rushed with the answer. You just need to convert $entityElementCount into string before using ctype_digit function, in order for code above to work.
spacemonkey
So, if (ctype_digit((string)$entityElementCount)) should do it.
Alex JL
+1  A: 
if(floor($number) == $number)

Is not a stable algorithm. When a value is matematically 1.0 the numerical value can be 0.9999999. If you apply floor() on it it will be 0 which is not equals to 0.9999999.

You have to guess a precision radius for example 3 digits

if(round($number,3) == round($number))
Notinlist
+2  A: 

If you know that it will be numeric (meaning it won't ever be a an integer cast as a string, like "ten" or "100", you can just use is_int():

$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;
$entityWholeNumber = is_int($entityElementCount);

echo ($entityWholeNumber) ? "Whole Number!" : "Not a whole number!";
Anthony
Good, but the ternary operator here is redundant. Just`$entityWholeNumber = is_int($entityElementCount)` is sufficient
Iain Fraser
I always do that. Good call.
Anthony
Your two code samples do exactly the same, the second just stores the result from the `is_int` call in a var (in a rather cumbersome way, I might add). Also, this checks if the var is an int, not if the value contained is a whole number.
Aistina
Integers are always whole numbers. If what he wants is to know if it's a whole number or a non-whole number, that means it's either an integer or a float. The code samples are supposed to do the same thing.
Anthony
by definition, integers are like whole number, but they can include negative sign. Whole numbers don't have negative sign.
ghostdog74
@ghostdog74 - Whole numbers can be negative. You're thinking of natural numbers, which also can't include 0. And the user didn't ask if it would be whole and real, just whole. Integers and whole numbers are the same thing. Not to mention that any of the rounding techniques offered would also not check if the number is less than 0.
Anthony
And please don't say "by definition" unless you can cite your dictionary. By definition integers are all whole numbers, including all natural numbers and their negative counterpart. Source: http://en.wikipedia.org/wiki/Integer
Anthony
+1  A: 

The basic way, as Chacha said is

if (floor($number) == $number)

However, floating point types cannot accurately store numbers, which means that 1 might be stored as 0.999999997. This will of course mean the above check will fail, because it will be rounded down to 0, even though for your purposes it is close enough to 1 to be considered a whole number. Therefore try something like this:

if (abs($number - round($number)) < 0.0001)
Aistina
I'd say this is the best solution, given the nature of floats. Just pick a precision level that suits your task.
Alex JL