Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2002
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Checking for a whole number

    Is there anyway I can check if a number is a whole number?

    I am retrieving images from a database and on every third image I want to start a new row in the table displaying them. I have set $i to be 0 and increased it with every reiteration of the loop. After having increased it I want to check if $i divided by 3 produces a whole number or not.

    How do I do this?

    Thanks
    WillowFae

  • #2
    New Coder
    Join Date
    Sep 2003
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you want the whole number every time:

    round($variable, 0);

    1.3423
    would return
    1

    IF, you just want to see if its a full number or has a .

    $blah = explode("\.", $variable);
    if $blah[1] == "" then
    the number is whole
    else then
    the number is not whole
    end if
    or something

  • #3
    New Coder
    Join Date
    Oct 2003
    Location
    AZ
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if(eregi("\.", $i))
    {
    echo "not a whole number";
    }
    else
    {
    echo "is a whole number";
    }

    if $i has a "." it will not be considered a whole number.
    PHP | FreeBSD | MySQL |
    Check documentation... check documentation again and actually read it..do it... doesn't work ask.

  • #4
    New Coder
    Join Date
    Oct 2003
    Location
    AZ
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    or you can just use

    <?php
    round($i);
    ?>

    which will round the number to a whole number.
    PHP | FreeBSD | MySQL |
    Check documentation... check documentation again and actually read it..do it... doesn't work ask.

  • #5
    New Coder
    Join Date
    Nov 2002
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ooh, thanks for those. Just what I wanted - the checking option, not the rounding up one.


  • #6
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    Originally posted by coffeedemon

    if $i has a "." it will not be considered a whole number. [/B]
    Ahh, but mathematically speaking, a whole number is...

    "a positive integer", so

    -1 although it would pass your code, is not a whole number. Neither is 0. So you would need to do an additional test to see if the number is > 0. If so, then it's whole.

    Just getting picky

  • #7
    New Coder
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Validate Whole Number in PHP

    Hi Friends,
    There is a Better and Faster Method than preg_match for whole number
    validation. that is : ctype_digit().... It take only String parameter , so make
    our input as String First like eg:

    $var = (string)$value;

    then You can Check like this:

    if (ctype_digit($var) ){
    print "whole number\n";
    }else{
    print "not whole number\n";
    }

    ok Be happy , if any clarification Revert to me
    anes.pa@gmail.com

    Bye Anes



  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    ...the hell? This is a 7 year old post!
    Are you a Help Vampire?


  •  

    Posting Permissions

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