我有一个代码,可以通过API更新Steam服务游戏中的物品列表。对此,这些价格来我往的样子:"price":0.64000000000000001,"count":897,"bprice":0.080000000000000002
。我怎样才能在API解析阶段这样做的价值price
和bprice
小数点后有2位?
我的解析代码:
public static function updatePrices()
{
if ($data = json_decode(file_get_contents(self::STEAMP)) ?? false) {
if ($data->success) {
$items_in_res = new \stdClass();
foreach($data->items as $key => $item) {
if ($item->count > 50 ) {
if ($item->bcount > 20) {
if ($item->price <= 1) {
$item->price = 0.64;
}
$items_in_res->$key = $item;
}
}
}
Storage::disk('local')->put(self::Prices, json_encode([
'time' => time(),
'items' => $items_in_res
]));
return $items_in_res;
} else {
\Log::error('Error: ' . $data->message);
return false;
}
} else {
\Log::error('Error updating prices!');
return false;
}
}
您可以number_format
为此使用。
echo number_format($item->price,2);
演示: https ://3v4l.org/NnZlb
就在方注意,number_format
也rounds silently
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句