PHP 7.1 使用 json_encode 函数造成浮点类型数据出现精度问题

2018-07-10
次阅读
1 分钟阅读时长

新项目用的 PHP 7.1.13 版本,在使用过程中发现 浮点类型 数据经过 json_encode 之后会出现精度问题。

举个例子:

$data = [
    'stock' => '100',
	'amount' => 10,
    'price' => 0.1
];

var_dump($data);

echo json_encode($data);

输出结果:

array(3) { 
    ["stock"]=> string(3) "100" 
    ["amount"]=> int(10) 
    ["price"]=> float(0.1) 
}

{
    "stock":"100",
    "amount":10,
    "price":0.10000000000000001
}

网上说可以通过调整 php.iniserialize_precision (序列化精度) 的大小来解决这个问题。

; When floats & doubles are serialized store serialize_precision significant
; digits after the floating point. The default value ensures that when floats
; are decoded with unserialize, the data will remain the same.
; The value is also used for json_encode when encoding double values.
; If -1 is used, then dtoa mode 0 is used which automatically select the best
; precision.
serialize_precision = 17

按照说明,将这个值改为 小于 17 的数字就解决了这个问题。

后来又发现一个折中的办法,就是将 float 转为 string 类型。

$data = [
	'stock' => '100',
	'amount' => 10,
    'price' => (string)0.1
];

var_dump($data);

echo json_encode($data);

输出结果:

array(3) { 
    ["stock"]=> string(3) "100" 
    ["amount"]=> int(10) 
    ["price"]=> string(3) "0.1" 
    
} 

{
    "stock":"100",
    "amount":10,
    "price":"0.1"
}

这样子也解决了问题,但是总感觉不太方便,所以就有了这个函数。

/**
 * @param $data 需要处理的数据
 * @param int $precision 保留几位小数
 * @return array|string
 */
function fix_number_precision($data, $precision = 2)
{
    if(is_array($data)){
        foreach ($data as $key => $value) {
            $data[$key] = fix_number_precision($value, $precision);
        }
        return $data;
    }

    if(is_numeric($data)){
        $precision = is_float($data) ? $precision : 0;
        return number_format($data, $precision, '.', '');
    }

    return $data;
}

测试:

$data = [
	'stock' => '100',
	'amount' => 10,
    'price' => 0.1,
    'child' => [
    	'stock' => '99999',
		'amount' => 300,
	    'price' => 11.2,
    ],
];

echo json_encode(fix_number_precision($data, 3));

输出结果:

{
    "stock":"100",
    "amount":"10",
    "price":"0.100",
    "child":{
        "stock":"99999",
        "amount":"300",
        "price":"11.200"
    }
}

PS: php 版本 >= 7.1 均会出现此问题。

本文作者:她和她的猫
本文地址https://her-cat.com/posts/2018/07/10/accuracy-of-php71-json-encode/
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!