我從雅虎財經抓住銀的價值及其附加值0.10爲當前值..這正顯示出正確的結果..PHP數學函數導致錯誤的輸出
$yql_base_url = "http://query.yahooapis.com/v1/public/yql?";
$params = array(
'q' => 'select * from html where [email protected] and [email protected];',
'url' => 'http://finance.yahoo.com/q?s=XAGUSD%3DX&ql=1',
'xpath' => '//*[@id="yfs_g00_xagusd=x"]',
'env' => 'http://datatables.org/alltables.env',
'format' => 'json',
);
$yql_query_url = $yql_base_url . http_build_query($params);
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($session);
$phpObj = json_decode($json);
if ($phpObj->query->count > 0) {
$price = $phpObj->query->results->span->content;
}
?>
<div> <?php echo $price; ?> which is current value </div>
<div><?php echo $price + .10; ?> where 0.10 is added</div>
但是,當我嘗試黃金價格,結果顯示錯誤的輸出即1或2
$yql_base_url = "http://query.yahooapis.com/v1/public/yql?";
$params = array(
'q' => 'select * from html where [email protected] and [email protected];',
'url' => 'http://finance.yahoo.com/q?s=XAUUSD%3DX&ql=1',
'xpath' => '//*[@id="yfs_g00_xauusd=x"]',
'env' => 'http://datatables.org/alltables.env',
'format' => 'json',
);
$yql_query_url = $yql_base_url . http_build_query($params);
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($session);
$phpObj = json_decode($json);
if ($phpObj->query->count > 0) {
$price = $phpObj->query->results->span->content;
}
?>
<div> <?php echo $price; ?> which is current value</div>
<div> <?php echo $price + .10; ?> where 0.10 is added</div>
任何人都可以請幫我在這件事..任何建議
好吧,我做了ka嘗試,它真的解決了..但我需要顯示的價格逗號分隔..即1597.28是用str_replace()所做的輸出;但我需要在div顯示1,597.28(我再次需要在要顯示的結果中添加逗號)..你可以請建議 –
@coolnicq你可能會想要使用'number_format()'函數 - 我已經編輯了我的答案以包含一個示例,但請隨時查看它的手冊頁,以查看它接受的參數。 –