Skip to content
Advertisement

PHP | Make a validation check whether the number has a decimal point == x.00 or not

Em, I don’t know how to explain this. I hope you’ll get the point.

I’ve variables:

$a = 10; //int
$b = 2.5; //float
$c = $a * $b; //I know this return will be float: 25

From those variables, I want to make a statement as follows:

if (//the value of $c have a decimal point == x.00) {
    echo $c;
} else {
    echo '';
}

Did you get it? What I want is that when the decimal point of $c is x.00 (like 25.00, 10.00, etc), the $c will be printed. But if the decimal point is NOT x.00 (eg 25.50, 25.7, etc) then $c will NOT be printed.

I’ve read some references but still don’t understand how to do it.

Thank you. I hope you understand what I mean.

Advertisement

Answer

PHP has is_integer()https://www.php.net/manual/en/function.is-integer.php

Or if you want to check manually, then you can compare against the rounded down (floor) and rounded up (ceil) values:

if ($a==Floor($a) && $a==Ceil($a)){
 // Whole Number
} else {
 // Has decimal point value
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement