PHP 检查变量是否为整数

发布于 2024-08-19 17:09:37 字数 219 浏览 2 评论 0原文

我有这个 PHP 代码:

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

我想知道的是,如何检查 $entityElementCount 是整数 (2, 6, ...) 还是部分数 (2.33, 6.2, ...) 。

谢谢你!

I have this PHP code:

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

What i want to know is, how to check whether $entityElementCount is a whole number (2, 6, ...) or partial (2.33, 6.2, ...).

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(22

尐籹人 2024-08-26 17:09:37
if (floor($number) == $number)
if (floor($number) == $number)
雪落纷纷 2024-08-26 17:09:37

我知道这已经很旧了,但我想我应该分享一些我刚刚发现的东西:

使用 fmod< /a> 并检查 0

$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;
if (fmod($entityElementCount,1) !== 0.0) {
    echo 'Not a whole number!';
} else {
    echo 'A whole number!';
}

fmod 与 % 不同,因为如果你有一个分数,% 似乎对我不起作用(它返回 0...例如,echo 9.4 % 1;将输出0)。使用 fmod,您将得到分数部分。例如:

echo fmod(9.4, 1);

将输出 0.4

I know this is old, but I thought I'd share something I just found:

Use fmod and check for 0

$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;
if (fmod($entityElementCount,1) !== 0.0) {
    echo 'Not a whole number!';
} else {
    echo 'A whole number!';
}

fmod is different from % because if you have a fraction, % doesn't seem to work for me (it returns 0...for example, echo 9.4 % 1; will output 0). With fmod, you'll get the fraction portion. For example:

echo fmod(9.4, 1);

Will output 0.4

没有伤那来痛 2024-08-26 17:09:37
$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";
}
$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";
}
已下线请稍等 2024-08-26 17:09:37

我会使用 intval 函数,如下所示:

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

}

测试:

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)"

其他解决方案

1)

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

测试:

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

2)

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

极端情况:

$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)) {

测试:

var_dump(ctype_digit("0x539")); // prints "bool(false)"
var_dump(ctype_digit(10)); // prints "bool(false)"
var_dump(ctype_digit(0x53)); // prints "bool(false)"

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)"
凉城凉梦凉人心 2024-08-26 17:09:37

基本方式,正如 Chacha 所说,

if (floor($number) == $number)

但是,浮点类型无法准确存储数字,这意味着 1 可能会存储为 0.999999997。这当然意味着上面的检查将失败,因为它将向下舍入为 0,即使对于您的目的来说,它足够接近到 1 以被视为整数。因此尝试这样的事情:

if (abs($number - round($number)) < 0.0001)

The basic way, as Chacha said is

if (floor($number) == $number)

However, floating point types cannot accurately store numbers, which means that 1 might be stored as 0.999999997. This will of course mean the above check will fail, because it will be rounded down to 0, even though for your purposes it is close enough to 1 to be considered a whole number. Therefore try something like this:

if (abs($number - round($number)) < 0.0001)
自我难过 2024-08-26 17:09:37

我测试了所有提出的解决方案,其中提到了许多有问题的值,它们至少在一个测试用例中都失败了。使用 is_numeric($value) 开始检查 $value 是否是一个数字,可以减少许多解决方案的失败次数,但不会将任何解决方案变成最终解决方案:

$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);

function is_whole_number($value) {
    // Doing this prevents failing for values like true or "ten"
    if (!is_numeric($value)) {
        return false;
    }

    // @ghostdog74's solution fails for "10.00"
    // return (ctype_digit((string) $value));

    // Both @Maurice's solutions fails for "10.00"
    // return ((string) $value === (string) (int) $value);
    // return is_int($value);

    // @j.hull's solution always returns true for numeric values
    // return (abs($value) % 1 == 0 ? true : false);

    // @ MartyIX's solution fails for "10.00"
    // return ($value === intval($value));

    // This one fails for (-(4.42-5))/0.29
    // return (floor($value) == $value);

    // This one fails for 2
    // return ctype_digit($value);

    // I didn't understand Josh Crozier's answer

    // @joseph4tw's solution fails for (-(4.42-5))/0.29
    // return !(fmod($value, 1) != 0);

    // If you are unsure about the double negation, doing this way produces the same
    // results:
    // return (fmod($value, 1) == 0);

    // Doing this way, it always returns false 
    // return (fmod($value, 1) === 0);

    // @Anthony's solution fails for "10.00"
    // return (is_numeric($value) && is_int($value));

    // @Aistina's solution fails for 0.999999997
    // return (abs($value - round($value)) < 0.0001);

    // @Notinlist's solution fails for 0.999999997
    // return (round($value, 3) == round($value));
}

foreach ($test_cases as $test_case) {
    var_dump($test_case);
    echo ' is a whole number? ';
    echo is_whole_number($test_case) ? 'yes' : 'no';
    echo "\n";
}

我认为@Aistina 和 @Notinlist 提出的解决方案是最好的解决方案,因为它们使用错误阈值来确定值是否是整数。值得注意的是,它们对于表达式 (-(4.42-5))/0.29 的工作符合预期,而所有其他在该测试用例中都失败了。

我决定使用@Notinlist的解决方案,因为它的可读性:

function is_whole_number($value) {
    return (is_numeric($value) && (round($value, 3) == round($value)));
}

我需要测试值是否是整数、货币或百分比,我认为2位数字的精度就足够了,所以@Notinlist的解决方案符合我的需求。

运行此测试:

$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);

function is_whole_number($value) {
    return (is_numeric($value) && (round($value, 3) == round($value)));
}

foreach ($test_cases as $test_case) {
    var_dump($test_case);
    echo ' is a whole number? ';
    echo is_whole_number($test_case) ? 'yes' : 'no';
    echo "\n";
}

产生以下输出:

float(0.29)
 is a whole number? no
int(2)
 is a whole number? yes
int(6)
 is a whole number? yes
float(2.33)
 is a whole number? no
float(6.2)
 is a whole number? no
string(5) "10.00"
 is a whole number? yes
float(1.4)
 is a whole number? no
int(10)
 is a whole number? yes
string(2) "10"
 is a whole number? yes
float(10.5)
 is a whole number? no
string(5) "0x539"
 is a whole number? yes
bool(true)
 is a whole number? no
bool(false)
 is a whole number? no
int(83)
 is a whole number? yes
float(9.4)
 is a whole number? no
string(3) "ten"
 is a whole number? no
string(3) "100"
 is a whole number? yes
int(1)
 is a whole number? yes
float(0.999999997)
 is a whole number? yes
int(0)
 is a whole number? yes
float(0.0001)
 is a whole number? yes
float(1)
 is a whole number? yes
float(0.9999999)
 is a whole number? yes
float(2)
 is a whole number? yes

I tested all the proposed solutions with many problematic values mentioned, they all fail for at least one of the test cases. Start checking if $value is a number using is_numeric($value) reduces the number of failures for many solutions, but does not turn any solution into an ultimate one:

$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);

function is_whole_number($value) {
    // Doing this prevents failing for values like true or "ten"
    if (!is_numeric($value)) {
        return false;
    }

    // @ghostdog74's solution fails for "10.00"
    // return (ctype_digit((string) $value));

    // Both @Maurice's solutions fails for "10.00"
    // return ((string) $value === (string) (int) $value);
    // return is_int($value);

    // @j.hull's solution always returns true for numeric values
    // return (abs($value) % 1 == 0 ? true : false);

    // @ MartyIX's solution fails for "10.00"
    // return ($value === intval($value));

    // This one fails for (-(4.42-5))/0.29
    // return (floor($value) == $value);

    // This one fails for 2
    // return ctype_digit($value);

    // I didn't understand Josh Crozier's answer

    // @joseph4tw's solution fails for (-(4.42-5))/0.29
    // return !(fmod($value, 1) != 0);

    // If you are unsure about the double negation, doing this way produces the same
    // results:
    // return (fmod($value, 1) == 0);

    // Doing this way, it always returns false 
    // return (fmod($value, 1) === 0);

    // @Anthony's solution fails for "10.00"
    // return (is_numeric($value) && is_int($value));

    // @Aistina's solution fails for 0.999999997
    // return (abs($value - round($value)) < 0.0001);

    // @Notinlist's solution fails for 0.999999997
    // return (round($value, 3) == round($value));
}

foreach ($test_cases as $test_case) {
    var_dump($test_case);
    echo ' is a whole number? ';
    echo is_whole_number($test_case) ? 'yes' : 'no';
    echo "\n";
}

I think that solutions like the ones proposed by @Aistina and @Notinlist are the best ones, because they use an error threshold to decide whether a value is a whole number. It is important to note that they worked as expected for the expression (-(4.42-5))/0.29, while all the others failed in that test case.

I decided to use @Notinlist's solution because of its readability:

function is_whole_number($value) {
    return (is_numeric($value) && (round($value, 3) == round($value)));
}

I need to test if values are whole numbers, currency or percentage, I think 2 digits of precision is enough, so @Notinlist's solution fits my needs.

Running this test:

$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);

function is_whole_number($value) {
    return (is_numeric($value) && (round($value, 3) == round($value)));
}

foreach ($test_cases as $test_case) {
    var_dump($test_case);
    echo ' is a whole number? ';
    echo is_whole_number($test_case) ? 'yes' : 'no';
    echo "\n";
}

Produces the following output:

float(0.29)
 is a whole number? no
int(2)
 is a whole number? yes
int(6)
 is a whole number? yes
float(2.33)
 is a whole number? no
float(6.2)
 is a whole number? no
string(5) "10.00"
 is a whole number? yes
float(1.4)
 is a whole number? no
int(10)
 is a whole number? yes
string(2) "10"
 is a whole number? yes
float(10.5)
 is a whole number? no
string(5) "0x539"
 is a whole number? yes
bool(true)
 is a whole number? no
bool(false)
 is a whole number? no
int(83)
 is a whole number? yes
float(9.4)
 is a whole number? no
string(3) "ten"
 is a whole number? no
string(3) "100"
 is a whole number? yes
int(1)
 is a whole number? yes
float(0.999999997)
 is a whole number? yes
int(0)
 is a whole number? yes
float(0.0001)
 is a whole number? yes
float(1)
 is a whole number? yes
float(0.9999999)
 is a whole number? yes
float(2)
 is a whole number? yes
饮湿 2024-08-26 17:09:37

如果您知道它将是数字(意味着它永远不会是转换为字符串的整数,例如 "ten""100",您可以使用 is_int() :

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

echo ($entityWholeNumber) ? "Whole Number!" : "Not a whole number!";

If you know that it will be numeric (meaning it won't ever be a an integer cast as a string, like "ten" or "100", you can just use is_int():

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

echo ($entityWholeNumber) ? "Whole Number!" : "Not a whole number!";
若水微香 2024-08-26 17:09:37
if(floor($number) == $number)

不是一个稳定的算法。当某个值在数学上为 1.0 时,该数值可以是 0.9999999。如果对它应用Floor(),它将是0,不等于0.9999999。

您必须猜测精确半径,例如 3 位数字

if(round($number,3) == round($number))
if(floor($number) == $number)

Is not a stable algorithm. When a value is matematically 1.0 the numerical value can be 0.9999999. If you apply floor() on it it will be 0 which is not equals to 0.9999999.

You have to guess a precision radius for example 3 digits

if(round($number,3) == round($number))
鲜肉鲜肉永远不皱 2024-08-26 17:09:37
    $num = 2.0000000000001;
    if( $num == floor( $num  ) ){
        echo('whole');
    }else{
        echo('fraction');
    }

例如:

2.0000000000001 |分数

2.1 |分数

2.00 |整个

2 |所有的

    $num = 2.0000000000001;
    if( $num == floor( $num  ) ){
        echo('whole');
    }else{
        echo('fraction');
    }

EX:

2.0000000000001 | fraction

2.1 | fraction

2.00 | whole

2 | whole

删除→记忆 2024-08-26 17:09:37

我想出的另一种黑客方法是ceil($value) === Floor($value)。如果数字是整数,则这应该始终为真,即使将 10 与 10.000 进行比较,甚至可以处理字符串中的数字,例如 ceil("10.0") === Floor(10).

Another hacky way I came up with is ceil($value) === floor($value). If a number is a whole number, this should always be true, even if comparing 10 with 10.000 and will even work with numbers cast in string, for example ceil("10.0") === floor(10).

本宫微胖 2024-08-26 17:09:37
(string)floor($pecahformat[3])!=(string)$pecahformat[3]
(string)floor($pecahformat[3])!=(string)$pecahformat[3]
回梦 2024-08-26 17:09:37

@Tyler Carter 解决方案的改进版本,它比原始解决方案更好地处理边缘情况:

function is_whole_number($number){
    return (is_float(($f=filter_var($number,FILTER_VALIDATE_FLOAT))) && floor($f)===$f);    
}

(Tyler 的代码无法识别字符串“123foobar”不是整数。这个改进版本不会犯这个错误。归功于@Shafizadeh发现错误的注释也是 php7 strict_types=1 兼容的)

improved version of @Tyler Carter's solution, which handles edge cases better than the original:

function is_whole_number($number){
    return (is_float(($f=filter_var($number,FILTER_VALIDATE_FLOAT))) && floor($f)===$f);    
}

(Tyler's code fail to recognize that the string "123foobar" is not a whole number. this improved version won't make that mistake. credits to @Shafizadeh in the comments for discovering the bug. also this is php7 strict_types=1-compatible)

森末i 2024-08-26 17:09:37
floor($entityElementCount) == $entityElementCount

如果这是一个整数,则为真

floor($entityElementCount) == $entityElementCount

This will be true if this is a whole number

一城柳絮吹成雪 2024-08-26 17:09:37

这并不是试图回答这个问题。他们已经有很多答案了。如果您按照问题所暗示的那样进行统计,我怀疑@antonio-vinicius-menezes-medei 的答案最适合您。但是我需要这个答案来进行输入验证。我发现此检查对于验证输入字符串是否为整数更可靠:

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

'is_numeric' 只是纠正 preg_match 中“true”转换为“1”的情况。

所以就用@antonio-vinicius-menezes-medei 的回答。我写了一个脚本来测试下面的内容。请注意 ini_set(' precision', 20)。 preg_match 会将参数转换为字符串。如果您的精度设置为低于浮点值的长度,它们将简单地按给定的精度舍入。与 @antonio-vinicius-menezes-medei 答案类似,此精度设置将强制类似的估计长度。

  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';
  }

产生以下输出:

float(0.28999999999999998002)
int(2)
int(6)
浮点(2.3300000000000000711)
浮点(6.2000000000000001776)
字符串(5) "10.00"
浮点数(1.39999999999999999112)
int(10)
字符串(2) "10"
浮动(10.5)
字符串(5)“0x539”
bool(true)
bool(false)
int(83)
浮点(9.4000000000000003553)
string(3) "十"
字符串(3)“100”
int(1)
浮点(0.99999999699999997382)
int(0)
浮点(0.00010000000000000000479)
浮点(1)
浮点数(0.99999990000000005264)
浮点(2.0000000000000004441)

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

浊酒尽余欢 2024-08-26 17:09:37

只是为了与本地化字符串/数字分享我的解决方案,这个组合对我来说就像一个魅力。

public static function isWholeNumber ($input, $decimalDelimiter = ',')
{
    if (is_string($input)){
        $input = str_replace($decimalDelimiter, '.', $input);
        $input = floatval($input);
    }

    if (fmod($input,1) !== 0.0) {
        return false;
    }

    return true;
}

Just to share my solution with localized string/number, this combo worked like a charm for me.

public static function isWholeNumber ($input, $decimalDelimiter = ',')
{
    if (is_string($input)){
        $input = str_replace($decimalDelimiter, '.', $input);
        $input = floatval($input);
    }

    if (fmod($input,1) !== 0.0) {
        return false;
    }

    return true;
}
嘴硬脾气大 2024-08-26 17:09:37

试试这个

if ($your_number % ceil($your_number) == 0) {

  //Number is whole

} else {

  //Number is not whole

}

Try this

if ($your_number % ceil($your_number) == 0) {

  //Number is whole

} else {

  //Number is not whole

}
倒带 2024-08-26 17:09:37

看似简单的方法是使用模数 (%) 来确定值是否为整数。

x = y % 1  

如果 y 是整数以外的值,则结果不是零 (0)。那么测试将是:

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. 

假设这会将负数视为整数,然后将 ABS() 包裹在 y 周围以始终测试正数。

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.

赠我空喜 2024-08-26 17:09:37

我总是使用类型转换来检查变量是否包含整数,当您不知道值的来源或类型时,这很方便。

if ((string) $var === (string) (int) $var) {
    echo 'whole number';
} else {
    echo 'whatever it is, it\'s something else';
}

在您的特定情况下,我会使用 is_int()

if (is_int($var) {
    echo 'integer';
}

I always use typecasting to check if variables contain a whole number, handy when you don't know the origin or type of the value.

if ((string) $var === (string) (int) $var) {
    echo 'whole number';
} else {
    echo 'whatever it is, it\'s something else';
}

In your particular case, I would use is_int()

if (is_int($var) {
    echo 'integer';
}
梦情居士 2024-08-26 17:09:37

仅适用于正整数的简单解决方案。这可能并不适用于所有情况。

$string = '0x539';
$ceil = ceil($string);

if($ceil < 1){
  $ceil = FALSE; // or whatever you want i.e 0 or 1
}

echo $ceil; // 1337

如果需要,您可以使用 Floor() 而不是 ceil()。

A simple solution for positive whole numbers only. This may not work for everything.

$string = '0x539';
$ceil = ceil($string);

if($ceil < 1){
  $ceil = FALSE; // or whatever you want i.e 0 or 1
}

echo $ceil; // 1337

You can use floor() instead of ceil() if so desired.

素染倾城色 2024-08-26 17:09:37
function isInteger($value)
{
    // '1' + 0 == int, '1.2' + 0 == float, '1e2' == float
    return is_numeric($value) && is_int($value + 0);
}

function isWholeNumber($value)
{
    return is_numeric($value)
        && (is_int($value + 0)
            || (intval($value + 0) === intval(ceil($value + 0))));
}

如果您想检查整数和小数,可以执行以下操作:

if (isInteger($foo))
{
    // integer as int or string
}
if (isWholeNumber($foo))
{
    // integer as int or string, or float/double with zero decimal part
}
else if (is_numeric($foo))
{
    // decimal number - still numeric, but not int
}

这将正确检查您的数字,而无需四舍五入、将其转换为 int (在小数的情况下将丢失小数部分)或执行 以下操作:任何数学。但是,如果您想将 1.00 视为整数,那么那就完全是另一回事了。

function isInteger($value)
{
    // '1' + 0 == int, '1.2' + 0 == float, '1e2' == float
    return is_numeric($value) && is_int($value + 0);
}

function isWholeNumber($value)
{
    return is_numeric($value)
        && (is_int($value + 0)
            || (intval($value + 0) === intval(ceil($value + 0))));
}

If you want to check for both whole and decimal numbers, you can do the following:

if (isInteger($foo))
{
    // integer as int or string
}
if (isWholeNumber($foo))
{
    // integer as int or string, or float/double with zero decimal part
}
else if (is_numeric($foo))
{
    // decimal number - still numeric, but not int
}

This will correctly check your number without rounding it, casting it to int (which in the case of a decimal number will lose the decimal part), or doing any math. If, however, you want to treat 1.00 as a whole number, then that's a whole another story.

梦明 2024-08-26 17:09:37

我知道这是一篇非常旧的帖子,但这是一个简单的函数,它将返回一个有效的整数并将其转换为 int。如果失败则返回 false。

function isWholeNumber($v)
{
    if ($v !='' && is_numeric($v) && strpos($v, '.') === false) {
        return (int)$v;
    }
    return false;
}

用法 :

$a = 43;
$b = 4.3;
$c = 'four_three';

isWholeNumber($a) // 43
isWholeNumber($b) // false
isWholeNumber($c) // false

I know this is a super old post but this is a simple function that will return a valid whole number and cast it to an int. Returns false if it fails.

function isWholeNumber($v)
{
    if ($v !='' && is_numeric($v) && strpos($v, '.') === false) {
        return (int)$v;
    }
    return false;
}

Usage :

$a = 43;
$b = 4.3;
$c = 'four_three';

isWholeNumber($a) // 43
isWholeNumber($b) // false
isWholeNumber($c) // false
苹果你个爱泡泡 2024-08-26 17:09:37
$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;

Method 1-
    By using ctype_digit() function. 

    if ( ctype_digit($entityElementCount )) { 
        echo "Whole Number\n"; 
    } else { 
        echo "Not a whole Number\n"; 
    } 


Method 2-
    By using is_float() function. 

    if (is_float($entityElementCount )) { 
        echo "Not a Whole Number\n"; 
    } else { 
        echo "Whole Number\n"; 
    } 


Method 3-
    By using is_int() function. 

    if (is_int($entityElementCount )) { 
        echo "Whole Number\n"; 
    } else { 
        echo "Not a whole Number\n"; 
    } 


Method 5-
    By using fmod() function. 

    It needs 2 parameters one dividend and other is divisor
    Here $dividend=$entityElementCount and divisor=1
    if (fmod($dividend,$divisor) !== 0.0) {
        echo 'Not a whole number!';
    } else {
     echo 'A whole number!';
    }

there are some more function like intval(), floor(),... can be used to check it`enter code here`
$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;

Method 1-
    By using ctype_digit() function. 

    if ( ctype_digit($entityElementCount )) { 
        echo "Whole Number\n"; 
    } else { 
        echo "Not a whole Number\n"; 
    } 


Method 2-
    By using is_float() function. 

    if (is_float($entityElementCount )) { 
        echo "Not a Whole Number\n"; 
    } else { 
        echo "Whole Number\n"; 
    } 


Method 3-
    By using is_int() function. 

    if (is_int($entityElementCount )) { 
        echo "Whole Number\n"; 
    } else { 
        echo "Not a whole Number\n"; 
    } 


Method 5-
    By using fmod() function. 

    It needs 2 parameters one dividend and other is divisor
    Here $dividend=$entityElementCount and divisor=1
    if (fmod($dividend,$divisor) !== 0.0) {
        echo 'Not a whole number!';
    } else {
     echo 'A whole number!';
    }

there are some more function like intval(), floor(),... can be used to check it`enter code here`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文