PHP check if variable is a whole number

后端 未结 20 965
感动是毒
感动是毒 2020-12-29 20:05

I have this PHP code:

$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;

What i want to know is, how to check whether $

相关标签:
20条回答
  • 2020-12-29 20:38

    What seems a simple approach would be to use modulus (%) to determine if a value is whole or not.

    x = y % 1  
    

    if y is anything other then a whole number the result is not a zero (0). A test then would be:

    if (y % 1 == 0) { 
       // this is a whole number  
    } else { 
       // this is not a whole number 
    }
    
    var isWhole = (y % 1 == 0? true: false);  // to get a boolean return. 
    

    Granted this will view a negative number as a whole number, then then just wrap ABS() around y to always test on the positive.

    0 讨论(0)
  • 2020-12-29 20:40
    (string)floor($pecahformat[3])!=(string)$pecahformat[3]
    
    0 讨论(0)
  • 2020-12-29 20:41

    I would use intval function like this:

    if($number === intval($number)) {
    
    }
    

    Tests:

    var_dump(10 === intval(10));     // prints "bool(true)"
    var_dump("10" === intval("10")); // prints "bool(false)"
    var_dump(10.5 === intval(10.5)); // prints "bool(false)"
    var_dump("0x539" === intval("0x539")); // prints "bool(false)"
    

    Other solutions

    1)

    if(floor($number) == $number) {   // Currently most upvoted solution: 
    

    Tests:

    $number = true;
    var_dump(floor($number) == $number); // prints "bool(true)" which is incorrect.
    

    2)

    if (is_numeric($number) && floor($number) == $number) {
    

    Corner case:

    $number = "0x539";
    var_dump(is_numeric($number) && floor($number) == $number); // prints "bool(true)" which depend on context may or may not be what you want
    

    3)

    if (ctype_digit($number)) {
    

    Tests:

    var_dump(ctype_digit("0x539")); // prints "bool(false)"
    var_dump(ctype_digit(10)); // prints "bool(false)"
    var_dump(ctype_digit(0x53)); // prints "bool(false)"
    
    0 讨论(0)
  • 2020-12-29 20:41
    floor($entityElementCount) == $entityElementCount
    

    This will be true if this is a whole number

    0 讨论(0)
  • 2020-12-29 20:43
    $entityElementCount = (-($highScore-$totalKeywordCount))/0.29;
    if (ctype_digit($entityElementCount) ){
        // (ctype_digit((string)$entityElementCount))  // as advised.
        print "whole number\n";
    }else{
        print "not whole number\n";
    }
    
    0 讨论(0)
  • 2020-12-29 20:45

    This is not an attempt to answer this question so much. Their are plenty of answer already. If you are doing statistics as the question implies I suspect @antonio-vinicius-menezes-medei answer will suite you best. However I needed this answer for input validation. I found this check more reliable for validating an input string is a whole number:

    is_numeric($number) && preg_match('/^[0-9]+$/', $number)

    The 'is_numeric' simply corrects for "true" converting to "1" in preg_match.

    So playing off of @antonio-vinicius-menezes-medei answer. I wrote a script to test this below. Note the ini_set('precision', 20). preg_match will convert the argument to a string. If your precicion is set below the length of the float values they will simply round at the given precision. Similar to @antonio-vinicius-menezes-medei answer this precision setting will force a similar estimation length.

      ini_set('precision', 20);
      $test_cases = array(0.29, 2, 6, 2.33, 6.2, '10.00', 1.4, 10, "10", 10.5, "0x539", true,
        false, 0x53, 9.4, "ten", "100", 1, 0.999999997, 0, 0.0001, 1.0, 0.9999999,
        (-(4.42-5))/0.29);
    
      foreach ($test_cases as $number)
      {
        echo '<strong>';
        var_dump($number);
        echo '</strong>';
        echo boolFormater(is_numeric($number) && preg_match('/^[0-9]+$/', $number));
        echo '<br>';
      }
    
      function boolFormater($value)
      {
        if ($value)
        {
          return 'Yes';
        }
        return 'No';
      }
    

    Which produces this output:

    float(0.28999999999999998002) No
    int(2) Yes
    int(6) Yes
    float(2.3300000000000000711) No
    float(6.2000000000000001776) No
    string(5) "10.00" No
    float(1.3999999999999999112) No
    int(10) Yes
    string(2) "10" Yes
    float(10.5) No
    string(5) "0x539" No
    bool(true) No
    bool(false) No
    int(83) Yes
    float(9.4000000000000003553) No
    string(3) "ten" No
    string(3) "100" Yes
    int(1) Yes
    float(0.99999999699999997382) No
    int(0) Yes
    float(0.00010000000000000000479) No
    float(1) Yes
    float(0.99999990000000005264) No
    float(2.0000000000000004441) No

    0 讨论(0)
提交回复
热议问题