php将浮点数与输入字段值进行比较
我有一个表单,用户可以在其中输入浮点值。我将这些值发布到php脚本中,并比较用户输入的数字是否在某些值之间。如果我发布了一个整数,那么无论该数字是否超出边界,比较都会返回true。如果输入一个浮点数,无论该数字是否在边界内,比较都会失败。我不是笨蛋,我已经在C++中做了浮点比较,我知道如何做一个if FLUAT1>= FLUAT2返回false……/P> 这是我的密码:php将浮点数与输入字段值进行比较,php,comparison,floating-point,concrete5,Php,Comparison,Floating Point,Concrete5,我有一个表单,用户可以在其中输入浮点值。我将这些值发布到php脚本中,并比较用户输入的数字是否在某些值之间。如果我发布了一个整数,那么无论该数字是否超出边界,比较都会返回true。如果输入一个浮点数,无论该数字是否在边界内,比较都会失败。我不是笨蛋,我已经在C++中做了浮点比较,我知道如何做一个if FLUAT1>= FLUAT2返回false……/P> 这是我的密码: //loading the helper $val = Loader::helper('synerciel_form','sy
//loading the helper
$val = Loader::helper('synerciel_form','synerciel_client');
//adding the fields to the inputs array for validation
$val->between('isolation_des_murs_peripheriques', 2.8, null, t($between.'Isolation des murs
pèriphèriques'), true, false);
//助手类
class SynercielFormHelper extends ValidationFormHelper {
const VALID_BETWEEN = 7;
const VALID_FLOAT = 7;
private $min;
private $max;
private $includeLow;
private $includeHigh;
public function between($field, $min, $max, $errorMsg, $includeLow = true, $includeHigh = true) {
$const = SynercielFormHelper::VALID_BETWEEN;
$this->min = $min;
$this->max = $max;
$this->includeLow = $includeLow;
$this->includeHigh = $includeHigh;
$this->addRequired($field, $errorMsg, $const);
}
...
public function test() {
$between = new ValidationNumbersBetweenHelper();
if (!$between->between($this->data[$field], $this->min, $this->max, $this->includeLow, $this->includeHigh)) {
$this->fieldsInvalid[] = $f;
}
}
我相信我的验证方法是棘手的部分
class ValidationNumbersBetweenHelper {
public function between($data, $min = null, $max = null, $includeLow = true, $includeHigh = true) {
if ($min && $includeLow) {
if (!($data >= $min))
return false;
} else if ($min) {
if (!($data > $min))
return false;
}
if ($max && $includeHigh) {
if (!($data <= $max))
return false;
} else if ($max) {
if (!($data < $max))
return false;
}
return true;
}
}
检查警告消息 您可以使用BC数学函数
希望这有助于尝试隔离麻烦代码。将验证函数放入一个独立的PHP文件中,并在其中进行测试。尝试检查$max和$min以获取!==null,因为0也是false。你可以颠倒逻辑,删除所有的错误!se、 g.更改>=添加了前面的2个代码片段,以便您了解我将变量存储在何处以及调用的总体流程……我相信最后一个被截断的代码应该是您感兴趣的代码
$status = bccomp($left, $right);
if ($status == 0) {
echo 'equal';
} else if ($status > 0) {
echo 'left bigger than right';
} else {
echo 'right bigger than left';
}