我使用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