# Thread: finding integers not straightforward

1. ## finding integers not straightforward

ok this is kind of a strange one >>

..I'm trying to check to see if a string is a integer however the string has a calculation that makes it an integer, e.g.

PHP Code:
for (\$d=1\$d<=100\$d++) {
if((
\$num*\$d)==(int)(\$num*\$d)) {

// perform action

break;
}

..now every number with 2 decimal points should fit this condition and perform the given action however numbers 1.09-1.15 (2dp) do not unexplicably.. so that has led me to the 'is_int' function but...

PHP Code:
\$ex = (1.05*100);

if(
is_int(\$ex)) {
echo
"yes it is";
} else {
echo
"no you don't!";

.. as you can see this doesn't work when a string is a calculation so I'm a little stuck on this and therefore call upon the good people of this forum <<

_thanks to all

2. If it has a decimal point, it's not an integer, it's a floating point number ("float"). Checking if a string is an integer is always going to fail, because they are different types. If you want to see if a string is a "number", use the is_numeric function.

Also, I suggest you read up on Types.

3. Originally Posted by Raffles
If it has a decimal point, it's not an integer, it's a floating point number ("float"). Checking if a string is an integer is always going to fail, because they are different types. If you want to see if a string is a "number", use the is_numeric function.

Also, I suggest you read up on Types.
.. perhaps the first piece of code demonstrates what I want to do the best > I want to find the lowest number (\$d) that \$num*\$d is an integer with \$num being to 2 decimal points, i.e. any number to 2dp multiplied by a number upto 100 will become an integer.

.. in the 2nd piece of code I'm performing the calculation 1.05*100 which is of course equal to 105 which is an integer - but possibly for some reason it remains 105.00 and that's why is_int doesn't work for me with this..

I'm hoping this is now a little clearer <<

4. After you multiply a float by 100, even though it's a "whole number", within the PHP world, it remains a float. I see what you want to do now. I think the issue might be to do with floating point precision (a horrible issue - read about in the Types page I linked to). Try this:

PHP Code:
for (\$d=1\$d<=100\$d++) {

if((
\$num*\$d)==(int)(string)(\$num*\$d)) {

// perform action

break;

}

5. Originally Posted by Raffles
After you multiply a float by 100, even though it's a "whole number", within the PHP world, it remains a float. I see what you want to do now. I think the issue might be to do with floating point precision (a horrible issue - read about in the Types page I linked to). Try this:

PHP Code:
for (\$d=1\$d<=100\$d++) {

if((
\$num*\$d)==(int)(string)(\$num*\$d)) {

// perform action

break;

}

Thanks for the replies Raffles >> for some reason it was still missing 1.13 and 1.14 but it seems to be working for all of them (yet to be 100%) with this:

PHP Code:
for (\$d=1\$d<=100\$d++) {

if((string)(
\$num*\$d)==(int)(string)(\$num*\$d)) {

// perform action

break;

}

.. although I still don't know why it has to have the (string) part in..

_thanks again

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•