通过PDO将双精度插入MySQL时精度损失 [英] Precision loss whilst inserting a double into MySQL through PDO

查看:88
本文介绍了通过PDO将双精度插入MySQL时精度损失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遇到了这种非常烦人的行为,我想知道我是在做错什么,还是故意的(如果是,为什么).

I've run into this really annoying behavior and I want to know if I'm doing something wrong, or if this is intentional (and if so, why).

每当我在php(5.3)中具有类型为double的变量并将其插入到类型为double的字段中的数据库(MYSQL 5.0)中时,该值总是四舍五入到后面的6位数字我使用PDO时的重点.所以下面的代码:

Whenever I have a variable in php (5.3) that is of type double and I want to insert it into the database (MYSQL 5.0) in a field that is of type double, the value always gets rounded down to 6 digits behind the point when I'm using PDO. So the below code:

$stmt = $pdo->prepare("UPDATE someTable SET someDouble = :somePHPDouble;");
$number = 0.11124379542256;
$stmt->bindValue(':somePHPDouble', $number);
$stmt->execute();

结果将0.111244插入db.但是,当我在绑定表达式中将变量转换为字符串(!)时:

Results in 0.111244 inserted into the db. But when I cast the variable to a string(!) in the binding expression like:

$stmt->bindValue(':somePHPDouble', (string)$number);

将其正确插入为0.11124379542256.

it inserts it properly as 0.11124379542256.

这是怎么回事?我一无所知. someDouble的MySQL数据类型实际上是一个double,并且在通过mysql控制台插入它时就可以使用.而且php中的变量确实是一个双精度变量,因此在我看来PDO内出现了问题.

What is happening here ? I'm clueless. MySQL datatype of someDouble really is a double, and when inserting it through mysql console it just works. And the variable in php really is a double, so it seems to me that something goes wrong inside PDO.

预先感谢, -CodePoet.

Thanks in advance, -CodePoet.

推荐答案

这既不是故意的,也不是您做错了什么.这似乎是一个PHP错误.

It is neither intentional, nor are you doing something wrong. It seems to be a PHP bug.

根据此错误报告,该问题已在PHP 5.2中修复. 11,但仅在5.3分支中才是最新版本,因此您可能需要对照此处提到的版本检查确切的PHP版本.

According to this bug report, it has been fixed for PHP 5.2.11, but only recently for the 5.3 branch, so you might want to check your exact PHP version against the ones mentioned there.

这篇关于通过PDO将双精度插入MySQL时精度损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆