I'm looking for a function that tests to see if an integer is a whole number. So far I've got this:
if (is_numeric($answer3)) {
echo "Is triangular\n";
} else {
echo "Is not triangular\n";
}
But this always returns "Is triangular", even when $answer3 is a decimal.
How can I test an integer to see if it has a decimal?
Say, “Whole numbers are numbers like 1, 3, 17, or 45. There aren’t any parts of the numbers like fractions or decimals.” Terms and Definitions: Whole number– A number that doesn’t have any fractional parts (or decimals) and is not negative.
Whole Number: It is defined as a set of positive integers which includes 0 in that. Those numbers start from 0 to infinity. Decimal: A number that has a decimal point followed by digits that show the fractional part. The combination of both numbers is the mixed number.
Whole Numbers vs. Fractions or Decimals Say, “Whole numbers are numbers like 1, 3, 17, or 45. There aren’t any parts of the numbers like fractions or decimals.” Terms and Definitions: Whole number– A number that doesn’t have any fractional parts (or decimals) and is not negative.
(1) Click the Browse button and select the data range that you will filter. (2) Move mouse over the Or to display the filter criteria section, and click the first box and specify the column that you will filter by whole number or decimal number; (3) Click the second box and select the Text from the drop down list;
is_numeric will return true for floats too, considering floats are numeric
Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.
You can try is_float(), but if the input is a string it wont work.
var_dump( is_float( '23.5' ) ); // return false
So if you are dealing with something that is a string representation of a number then just look for a .
if ( strpos( $answer3, '.' ) === false )
You can add is_numeric if you need to
// Make sure its numeric and has no decimal point
if ( is_numeric( $answer3 ) && strpos( $answer3, '.' ) === false )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With