SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    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. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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. #5
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    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


Bookmarks

Posting Permissions

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