PHP检查变量是否为整数
我有以下PHP代码:PHP检查变量是否为整数,php,integer,double,numbers,Php,Integer,Double,Numbers,我有以下PHP代码: $entityElementCount = (-($highScore-$totalKeywordCount))/0.29; 我想知道的是,如何检查$entityElementCount是整数(2,6,…)还是部分数(2.33,6.2,…) 谢谢大家! if (floor($number) == $number) 如果这是一个整数,则为真 $entityElementCount = (-($highScore-$totalKeywordCount))/0.29; if
$entityElementCount = (-($highScore-$totalKeywordCount))/0.29;
我想知道的是,如何检查$entityElementCount是整数(2,6,…)还是部分数(2.33,6.2,…)
谢谢大家!
if (floor($number) == $number)
如果这是一个整数,则为真
$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";
}
不是一个稳定的算法。当值为1.0时,数值可以是0.9999999。如果对其应用floor(),则它将为0,而不是0.9999999
您必须猜测一个精确半径,例如3位数
if(round($number,3) == round($number))
如果您知道它将是数字的(这意味着它将永远不会是一个整型字符串,如
“十”
或“100”
,您可以使用is_int()
:
正如查查所说,最基本的方法是
if (floor($number) == $number)
但是,浮点类型无法准确地存储数字,这意味着1可能存储为0.99999997。这当然意味着上述检查将失败,因为它将被舍入为0,即使出于您的目的,它非常接近1,可以被视为一个整数。因此,请尝试以下操作:
if (abs($number - round($number)) < 0.0001)
if($number === intval($number)) {
}
if(绝对值($number-round($number))<0.0001)
一种看似简单的方法是使用模数(%)来确定值是否为整数值
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
始终测试正数。我知道这很旧,但我想我会分享我刚刚发现的一些东西:
使用并检查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
我将使用如下函数:
if (abs($number - round($number)) < 0.0001)
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)"
$number = true;
var_dump(floor($number) == $number); // prints "bool(true)" which is incorrect.
其他解决方案
1)
if(floor($number) == $number) { // Currently most upvoted solution:
if (is_numeric($number) && floor($number) == $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)"
$number = true;
var_dump(floor($number) == $number); // prints "bool(true)" which is incorrect.
2)
if(floor($number) == $number) { // Currently most upvoted solution:
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)
测试:
var_dump(ctype_digit("0x539")); // prints "bool(false)"
var_dump(ctype_digit(10)); // prints "bool(false)"
var_dump(ctype_digit(0x53)); // prints "bool(false)"
我总是使用类型转换来检查变量是否包含整数,这在您不知道值的来源或类型时非常方便
if ((string) $var === (string) (int) $var) {
echo 'whole number';
} else {
echo 'whatever it is, it\'s something else';
}
在你的特殊情况下,我会使用
我用提到的许多有问题的值测试了所有建议的解决方案,它们都在至少一个测试用例中失败。开始检查
$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";
}
我需要测试值是整数、货币还是百分比,我认为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
仅适用于正整数的简单解决方案。这可能并不适用于所有情况
$string = '0x539';
$ceil = ceil($string);
if($ceil < 1){
$ceil = FALSE; // or whatever you want i.e 0 or 1
}
echo $ceil; // 1337
$string='0x539';
$ceil=ceil($string);
如果($ceil<1){
$ceil=FALSE;//或任何您想要的值,即0或1
}
echo$ceil;//1337
如果需要,可以使用floor()而不是ceil()
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(在十进制数字的情况下,int将丢失小数部分)或进行任何数学运算。然而,如果你想把
1.00
当作一个整数,那就完全是另一回事了。这并不是为了回答这个问题。他们的答案已经很多了。如果你在做问题所暗示的统计,我怀疑@antonio vinicius menezes medei的答案最适合你。然而,我需要这个答案来进行输入验证。我发现,验证输入字符串是否为整数时,此检查更可靠:
是数值($number)和预匹配('/^[0-9]+$/',$number)
“is_numeric”简单地更正了preg_match中转换为“1”的“true”
因此,播放@antonio vinicius menezes medei的答案。我在下面编写了一个脚本来测试这一点。注意ini\u集('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';
}
ini_集('precision',20);
$test_cases=数组(0.29,2,6,2.33,6.2,10.00,1.4,10,10,10.5,0x539),真,
假,0x53,9.4,“十”,“100”,1,0.99999997,0,0.0001,1.0,0.999999,
(-(4.42-5))/0.29);
foreach($test_cases作为$number)
{
回声“”;
var_dump(数字);
回音“”;
回声布尔格式(是数值($number)和预匹配('/^[0-9]+$/',$number));
回声“
”;
}
函数布尔格式($value)
{
如果(价值)
{
返回“是”;
}
返回“否”;
}
这将产生以下输出:
浮动(0.2899999999998002)否int(2)是
int(6)是
浮动(2.330000000000000711)否
浮动(620000000000001776)否
字符串(5)“10.00”否
浮动(1.399999999999112)否
int(10)是
字符串(2)“10”是
浮动(10.5)否
字符串(5)“0x539”否
bool(真)No
bool(假)No
int(83)是
浮动(9.4000000000000003553)否
字符串(3)“十”否
字符串(3)“100”是
int(1)是
浮动(0.9999999996999997382)否
int(0)是
浮动(0.000100000000000000479)否
浮动(1)是
浮动(0.9999990000000005264)否
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';
}
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
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;
}
$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`
function is_whole_number($number){
return (is_float(($f=filter_var($number,FILTER_VALIDATE_FLOAT))) && floor($f)===$f);
}
$num = 2.0000000000001;
if( $num == floor( $num ) ){
echo('whole');
}else{
echo('fraction');
}