[英]Detect round number in PHP?
How can I find out whether a float in PHP is a round number? 如何判断PHP中的浮点数是否为整数?
is_round(1.5); // false
is_round(1.0); // true
is_round(1.00000001); // false
Modification to Rob's code regarding sterofrog's comment. 关于Sterofrog评论的Rob代码的修改。 Code checks to ensure the value is also numeric. 代码检查以确保值也是数字。
function is_round($value) {
return is_numeric($value) && intval($value) == $value;
}
function is_round( $value ) {
return intval( $value ) == $value;
}
A much shorter version: !fmod($num,1)
in PHP or this in JS: !($num % 1)
一个更短的版本: !fmod($num,1)
PHP中的!fmod($num,1)
或JS中的这个: !($num % 1)
As a (more readable) function: 作为(更易读)功能:
function is_round( $num ) {
return !fmod($num, 1);
}
This works on the basis that any number that's not exactly 0 is falsey. 这是基于任何不完全为0的数字都是假的。 You can use fmod($num,1)
to get the decimals of a number; 您可以使用fmod($num,1)
来获取数字的小数; once you have those you can just !
一旦你有那些你可以!
them. 他们。
if ($value = round($value))
应该管用。
thierryreeuwijk在此页面上指定的功能应指定您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.