In perl, I want to check if the given variable holds a floating point number of not. To check this I am using,
my $Var = 0.02 # Floating point number
if (int($Var) != $Var) {
# floating point number
}
But the above code will not work for 0.0,
How can I achieve this?
This is a FAQ. See How do I determine whether a scalar is a number/whole/integer/float?
You can also use Scalar::Util::Numeric.
Interestingly, while
prints
int(not surprising if you look at the source code), based on @tchrist’s comment, it should be possible to look at the information supplied in the variables structure to distinguish0from0.0:Output:
It seems to me that the check would have to just look at if the
NOKflag is set. It takes me ages to write even the simplest XS so I won’t provide an implementation.