Home >Backend Development >PHP Tutorial >字段类型decimal(10,1),计算100.0+1.5=101.0,为什么?

字段类型decimal(10,1),计算100.0+1.5=101.0,为什么?

WBOY
WBOYOriginal
2016-06-23 13:48:06961browse

$sql = "update ".$fdyu->table('users')." set yujia=yujia+".$yujia." where user_id=".$user_id;
$db->query($sql);

yujia字段类型为decimal(10,1),假如原yujia字段中的数值是100.0,现在我提交$yujia=1.5,结果yujia字段的值是101.0,少了0.5,为什么?


回复讨论(解决方案)

create temporary table t (n decimal(10,1));insert into t values (100.0);update t set n=n+1.5;select * from t

101.5 

我找到原因了,原来是我的变量用得是整型
$yujia  = isset($_REQUEST['yujia']) ? intval(trim($_REQUEST['yujia'])) : 0;

intval?整形??掉小?。。
改用floatval就可以了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn