为什么将十进制字符串转换为float会将值更改为最后一个小数位的一小部分?
例如,我有:
class API_something{
public function __construct($id, $name, $rank, $weight){
$this->id = (int)$id;
$this->name = strtolower($name);
$this->rank = (int)$rank;
$this->weight = floatval($weight);
}
}
此对象通过传递给$weight
的字符串进行实例化。这最终是json_encode
' d,值是这样的:
{
"weight": 81.770399999999995,
"id": 6,
"rank": 1,
"name": "ommitted"
}
我知道我可以使用number_format
或round
将其恢复到4位小数,但两个函数都返回一个字符串。
如果没有PHP更改值,有没有办法将原始字符串转换为float变量?