tags:

views:

95

answers:

3

How can I find out whether a float in PHP is a round number?

is_round(1.5); // false
is_round(1.0); // true
is_round(1.00000001); // false
+4  A: 
function is_round( $value ) {
    return intval( $value ) == $value;
}
Rob
this will return true for "foobar"
stereofrog
**@stereofrog:** How come? `intval('bar') != 'bar'` since `0 != 'bar'`.
Alix Axel
@alix, hard to believe, but in php, 0 == 'bar' :-o
stereofrog
@stereofrog: Wow...
Alix Axel
A: 

The function specified on this page by thierryreeuwijk should specify your needs.

Traveling Tech Guy
I'm not entirely sure, because the incoming variable is still a `float`. Or am I mistaken and this will work?
Pekka
+7  A: 

Modification to Rob's code regarding sterofrog's comment. Code checks to ensure the value is also numeric.

function is_round($value) {
    return is_numeric($value) && intval($value) == $value;
}
John Himmelman