Number.prototype.isInteger = Number.prototype.isInteger || function(x) {
return (x ^ 0) === x;
}
console.log(Number.isInteger(1));Run Code Online (Sandbox Code Playgroud)
将在IE10浏览器中抛出错误
我有一个页面(index.php)GET从URL 获取一个变量并检查它是否安全.该GET变量应该只是一个整数.我使用以下代码来检查这一点,但在所有情况下,整数与否,我得到index.php页面.标题永远不会出现.在此代码之后,页面的其余部分将以html标记开头显示.
PHP:
<?php ob_start(); session_start();
$q=trim($_GET['q']);
if (!is_numeric($q)){
header("HTTP/1.0 404 Not Found");
}
?>
Run Code Online (Sandbox Code Playgroud) 我有以下计算:
$this->count = float(44.28)
$multiple = float(0.36)
$calc = $this->count / $multiple;
$calc = 44.28 / 0.36 = 123
Run Code Online (Sandbox Code Playgroud)
现在我想检查我的变量$calc是否是整数(有小数).
我尝试过,if(is_int()) {}但这不起作用,因为$calc = (float)123.
也试过这个 -
if($calc == round($calc))
{
die('is integer');
}
else
{
die('is float);
}
Run Code Online (Sandbox Code Playgroud)
但这也行不通,因为它在每种情况下都会返回'is float'.在上面的情况下,这应该是真的,因为在舍入后123与123相同.
我想验证输入字段.用户应输入最小长度为10位的电话号码.
所以我需要检查非法字符.只检查输入是否为整数会很好.
我想出了这个,但它不起作用(n将是字符串).
function isInt(n){
return typeof n== 'number' && n%1==0;
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我遇到了一个问题,我需要确定用户输入的字段是整数还是浮点数.然后答案将预先选择沿着表格进一步下拉.经过多次挖掘后,我发现了许多框架代码,但没有一个实际上正确地完成了工作.我使用的测试数据是;
空白答案,非数字(Alpha),1.0,10,1.10,2.4,3.0,0.30,0.00
许多其他帖子也使用上述数据进行了测试,我找不到正确传递所有数据的帖子.
所以我写了下面的内容,以便它可以由你的好自己审查,希望如果他们遇到同样的情况,它会帮助其他人.
function isInteger(value)
{
//if(isNaN(value))return Nan;//optional check
//test for decimal point
if(!( /^-?\d+$/.test(String(value))))
{
//decimal point found
//if parseInt changes value it must be a float
if(parseInt(value) / 1 != value)return false;
}
//no decimal point so must be integer
return true;
}
Run Code Online (Sandbox Code Playgroud) 说我有一个包含数字的字符串.我想检查这个数字是否是整数.
IsInteger("sss") => false
IsInteger("123") => true
IsInterger("123.45") =>false
Run Code Online (Sandbox Code Playgroud) 我可以想到几种方法,例如.
Convert.ToInt32(floatingPoint) - floatingPoint == 0;
Math.Truncate(floatingPoint) - floatingPoint == 0;
floatingPoint % 1 == 0;
Math.Floor(floatingPoint) == floatingPoint;
//etc...
Run Code Online (Sandbox Code Playgroud)
但哪种方法最可靠?
在继续之前,我想确保文件夹具有正确的名称格式。尽管{char.IsDigit}不起作用,但是下面的代码演示了我正在尝试执行的操作。我想用“任何数字”代替char.IsDigit。
if(versionName == $"Release {char.IsDigit}.{char.IsDigit}.{char.IsDigit}.{char.IsDigit}")
{
//Do something
}
Run Code Online (Sandbox Code Playgroud)
谢谢