php,var_export 失败并出现 float

标签 php wtforms wtforms-json wtfpl

<分区>

很简单。考虑这段代码:

var_export (11.2);

返回

11.199999999999999

使用 PHP 5.6

怎么回事?

最佳答案

来自 php.net 手册页的评论:

Looks like since version 5.4.22 var_export uses the serialize_precision ini setting, rather than the precision one used for normal output of floating-point numbers. As a consequence since version 5.4.22 for example var_export(1.1) will output 1.1000000000000001 (17 is default precision value) and not 1.1 as before.

很高兴知道。我也不知道这个变化。

serialize_precision

Available since PHP 4.3.2. Until PHP 5.3.5, the default value was 100.

因此,我们可以获得我们熟悉的行为:ini_set('serialize_precision', 100);

警告

使用 ini_set() 时要非常小心,因为这可能会进一步改变代码的行为。一个“安全”的方法是使用这样的东西:

$storedValue = ini_get('serialize_precision');
ini_set('serialize_precision', 100);
// Your isolated code goes here e.g var_export($float);
ini_set('serialize_precision', $storedValue);

这可确保代码中进一步向下/更深层次的更改不受影响。

通常,在您的代码中使用 ini_set() 应该被认为是危险的,因为 它可能会产生严重的副作用。重构您的代码以在没有 ini_set() 的情况下运行通常是更好的选择。

关于php,var_export 失败并出现 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32149340/

相关文章:

php - SQL 行回显到 Javascript 时出现问题,错误 SyntaxError : unterminated string literal

php - MySQL DUMP 作为 CSV

javascript - Flask wtforms Integerfield() 返回 NaN

python - WTForms FieldList - 获取未排序的数据

python - 为什么我的 WTForms-JSON 表单无法正确更新?

php - 直接在 MySQL 中将多行从一个表复制到另一个表?

php - 不在对象上下文中时使用 $this 时出错

python - 使用 SQLAlchemy 覆盖 FlaskAdmin 的默认 WTF 表单验证

python - WTForms-JSON 不适用于 FormFields