Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect round number in PHP?

Tags:

php

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
like image 465
Pekka Avatar asked Feb 27 '10 22:02

Pekka


2 Answers

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;
}
like image 159
John Himmelman Avatar answered Sep 25 '22 07:09

John Himmelman


function is_round( $value ) {
    return intval( $value ) == $value;
}
like image 25
Rob Avatar answered Sep 21 '22 07:09

Rob