php number_格式给出错误消息
我正在使用php解析XML文件,但在尝试显示格式化数字时收到错误消息: 警告:number_format()要求参数1为双精度,第45行的/home/chesspro/public_html/UNYCL/people-8.php中给出的对象php number_格式给出错误消息,php,xml,Php,Xml,我正在使用php解析XML文件,但在尝试显示格式化数字时收到错误消息: 警告:number_format()要求参数1为双精度,第45行的/home/chesspro/public_html/UNYCL/people-8.php中给出的对象 <?php $xml = simplexml_load_file('Rochester_1.xml'); foreach($xml->meet as $item) { print '<table class="basic re
<?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>';}
?>
现在我能修好这个吗?我需要一个小数点精度(X.X),尽管有些数字是像“7”这样的整数,我想格式化并显示为“7.0”。请注意,我使用的是接近当前的php版本5.3.4。数字格式($X,1)。“”;
print '<td>'. number_format($X, 1) ."</td>";
看
如果这不起作用,请尝试类型转换:
$X=(浮动)$game->white->score;
打印“”。数字格式($X)。“”;
它看起来像$game->white->score
不返回float或int。
试着这样做
$X=(浮动)$game->white->score;
打印“”。数字格式($X)。“”代码>
但是带有一个参数的number_format()将返回以千为单位分组的数字,所以如果您想要像7.0一样,最好使用print'。数字格式($X,1)。“”代码>当您将其更改为:$subtotal=$subtotal+floatval((字符串)$temp->white->score)时,它是否有效代码>?绝对完美!结果是:谢谢你们!警告:浮子是危险的。例:与11不同,向浮子浇铸可能会产生10.99999999999
$X = (float)$game->white->score;
print '<td>'. number_format($X) ."</td>";