php将浮点数与输入字段值进行比较
我有一个用户可以输入浮点值的表单。我将这些值发布到 PHP 脚本中,然后比较用户输入的数字是否在某些值之间。如果我发布一个整数,无论数字是否超出边界,比较都会返回 true。如果我输入浮点数,无论该数字是否在边界内,比较都会失败。我不傻,我已经在 C++ 中完成了浮点比较,并且我知道如何执行 if( float1 >= float2) return false...
这是我的代码:
//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);
//helper class
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;
}
}
I have a form in which users can enter floating point values. I post these values to a php script and i compare if the numbers the users entered are between some values. If i post an integer the comparison returns true no matter if the number exceeded the boundaries. If i entere a floating point number the comparison fails no matter if the number is within the boundaries. I am not stupid, i've done floating point comparisons in c++ and i know how to do an if( float1 >= float2) return false...
here is my code:
//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);
//helper class
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;
}
}
My validation method (i believe here is the tricky part)
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查警告消息 http://php.net/manual/en/language.operators .comparison.php
您可以使用BC数学函数http://php.net/manual/en/function.bccomp.php
希望这有帮助
Check the warning message http://php.net/manual/en/language.operators.comparison.php
You can use BC Math Functions http://php.net/manual/en/function.bccomp.php
Hope this helps
尝试隔离有问题的代码。将验证函数放入独立的 PHP 文件中并在那里进行测试。尝试检查
$max
和$min
是否为!== null
,因为0
也是false.您可以反转逻辑并删除所有
!
。 (例如将>=
更改为<
),因此您可以使用“小于”来代替“不大于或等于”Try isolating the troublesome code. Put your validation function into a stand-alone PHP file and test it there. Try checking
$max
and$min
for!== null
as0
is alsofalse
. You could reverse the logic and remove all the!
s. (e.g. change>=
to<
) so instead of "not greater than or equal to" you can have "less than"