Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whole number or decimal number

Tags:

php

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?

like image 707
Max Chandler Avatar asked Jun 14 '12 21:06

Max Chandler


People also ask

What is a whole number?

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.

What is the difference between a whole number and decimal number?

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.

What is the difference between whole numbers and fractions?

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.

How to filter data by whole number or decimal number?

(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;


1 Answers

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 )
like image 177
Galen Avatar answered Oct 09 '22 14:10

Galen