我正在处理一个非常简单的API项目,在我使用json_encode()
之前,一切都按预期方式进行。当我去https://phoenix.ltda/api/index.php?version=1.3则返回CCD_ 2。如何删除多余的零,只得到1.4
?只有当我使用json_decode()
时才会出现此问题。我该怎么解决这个问题?我会使用PHP的round()
吗?
index.php
<?php
if(isset($_GET['version'])){
$current_version = $_GET['version'];
require_once 'includes/updates.php';
$next_version = get_updated_version($current_version);
$encode = array('version' => $next_version);
$next_version = json_encode($encode);
echo $next_version;
}
?>
更新.php
<?php
function get_updated_version($version){
$string = $version+'.1';
return $string;
}
?>
编辑:当我传递1.6和1.1等数字时,也会出现问题。
使用round()
清除多余的数字。
function get_updated_version($version){
$string = round($version + .1, 3);
return $string;
}
小数点后最多有3位数字。
请参阅浮点运算是否已损坏?根本原因。