Reputation: 5
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.
Upvotes: 0
Views: 618
Reputation: 78994
Much easier. Just check if the integer of the number is equal ==
to the number:
if ((int)$c == $c) {
echo $c;
} else {
echo '';
}
Upvotes: 0
Reputation: 2397
You can try like below.
<?php
function is_decimal_exist( $value ) {
return is_numeric( $value ) && floor( $value ) != $value;
}
$my_value = "10.00";
if( is_decimal_exist ( $my_value ) ) {
echo ''; // Decimal Present, Do Not Print $my_value
}else{
echo $my_value; // Print $my_value
}
?>
Upvotes: 0
Reputation: 620
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
}
Upvotes: 1