PHP&;JSON | JSON编码为一个值加上许多零
我正在做一个非常简单的API项目,所有的工作都按照它应该的方式进行,直到我使用PHP&;JSON | JSON编码为一个值加上许多零,php,json,Php,Json,我正在做一个非常简单的API项目,所有的工作都按照它应该的方式进行,直到我使用json\u encode()。当我转到它时,它返回{“version”:1.4000000000000001}。我如何删除额外的零并只获得1.4?只有在使用json\u decode()时才会出现此问题。我该如何解决这个问题?我会使用PHP的round() index.php <?php if(isset($_GET['version'])){ $current_version = $_GET['ve
json\u encode()
。当我转到它时,它返回{“version”:1.4000000000000001}
。我如何删除额外的零并只获得1.4
?只有在使用json\u 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
function get_updated_version($version){
$string = $version+'.1';
return $string;
}
?>
updates.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
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位数字
请参阅以了解根本原因。dojson\u encode(数组('version'=>(string)$next\u version))
或fix precision是