我使用PHP來解析XML文件,但我得到試圖顯示格式的數字時的錯誤消息:PHP number_format給出錯誤信息
警告:number_format()預計參數1雙待,對象給出在/home/chesspro/public_html/UNYCL/people-8.php在線45
<?php
$xml = simplexml_load_file('Rochester_1.xml');
foreach($xml->meet as $item) {
print '<table class="basic report tableland brown_gradient"><thead>';
print '<tr><th class="R-align">Board</th><th class="L-align">'.$item->visitor.'</th>';
$subtotal = 0;
foreach($item->match as $temp) {$subtotal = $subtotal + $temp->white->score;}
print "<th>" .number_format($subtotal,1). "</th>";
$subtotal = 0;
foreach($item->match as $temp) {$subtotal = $subtotal + $temp->black->score;}
print "<th>" .number_format($subtotal,1). "</th>";
print '<th class="L-align">'.$item->home.'</th>';
print '</tr><tbody>';
foreach($item->match as $game) {
print '<tr><td class="R-align">'. $game->board . "</td>";
print '<td>'. $game->white->player."</td>";
//Here is error: note that number is sometimes 0
$X = $game->white->score;
print '<td>'. number_format($X) ."</td>";
print '<td>'. $game->black->score."</td>";
print '<td class="L-align">'. $game->black->player."</td>";
print "</tr>";}
print '</table>';}
?>
現在可以解決這個問題?我需要小數精度(XX)中的一個點,儘管一些號碼,如「7」的整數,我想格式化而顯示爲「7.0」請注意,我用的是接近目前的PHP版本5.3.4
它的工作原理,當你將其更改爲:'$小計= $ subtotal + floatval((string)$ temp-> white-> score);'? – vstm 2011-12-25 06:32:09